How to dynamically assign a value to the commandName attribute in <form:form> tag of spring

StackOverflow https://stackoverflow.com/questions/16142122

سؤال

I want to dynamically add a string value to the commandName attribute using the following JSTL code.

<c:choose>
    <c:when test='${catalogueEntry==null}'>
     add_item
     </c:when>
    <c:otherwise>
     update_item
     </c:otherwise>
</c:choose>

Looks like the spring is interpreting the whole code as a string value instead of assigning the value 'add_item' or 'update_item'. I tried using the javascript code as well but figured out it is giving the same error as it is client side technology. Can anyone show me where my problem is?

هل كانت مفيدة؟

المحلول

Simply make use of EL ternary operator. In your case code can be written as:

<form:form  method="post" commandName="${(catalogueEntry==null) ? 'add_item' : 'update_item'}">
    ...
</form:form>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top