Home > database >  JSP Form how to add parameter based on url IF it exists?
JSP Form how to add parameter based on url IF it exists?

Time:11-02

I have JSP like this:

<form action="study.show" method="POST>
  <input type="hidden" name="arg0" value="${param.arg0}" />
  <input type="hidden" name="arg" value="${param.arg}" />
  ...

URL sometimes have arg0 and arg in the url and sometimes not. Therefore, if there is arg0 and arg, it make POST request something like this:

.../study.show?arg0=123abc&arg=arg

However, if it doesn't exist, it still send it with parameter:

.../study.show?arg0=&arg=

How can I append parameter only when it exists so it is like this:

.../study.show

CodePudding user response:

After few hours of research, I found I can do this:

    <c:if test="${not empty param.arg0}">
        <input type="hidden" name="arg0" value="${param.arg0}" />
    </c:if>
    <c:if test="${not empty param.arg}">
        <input type="hidden" name="arg" value="${param.arg}" />
    </c:if>
  •  Tags:  
  • jsp
  • Related