简体   繁体   中英

How to use JSTL c:choose to change a form action

I have a form action on my jsp page, which I would like the action to change depending on what the "user" being manipulated is.

<c:choose>
        <c:when test="${user.user_username==username}">
            <form method="post" action="myinfo" class="form-horizontal">
        </c:when>   
        <c:otherwise>
            <form method="post" action="updated" class="form-horizontal">
        </c:otherwise>
</c:choose>

I have a submit button on the bottom of the page. Depending the user being manipulated, I would like to redirect the page to "myinfo" or "updated". However I keep getting an error saying that form has no end tag . I have it closed on the very bottom but I guess that's not the right way to do it.

Use <c:choose> to set the action into a variable, then EL to use it in a form.

<c:choose>
  <c:when test="${user.user_username==username}">
    <c:set var="formAction" value="myinfo" />
  </c:when>   
  <c:otherwise>
    <c:set var="formAction" value="updated" />
  </c:otherwise>
</c:choose>
<form method="post" action="${formAction}" class="form-horizontal">
  ...
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM