【问题标题】:How to dynamically assign a value to the commandName attribute in <form:form> tag of spring如何为spring的<form:form>标签中的commandName属性动态赋值
【发布时间】:2013-04-15 01:27:10
【问题描述】:

我想使用以下 JSTL 代码动态地将字符串值添加到 commandName 属性。

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

看起来 spring 将整个代码解释为字符串值,而不是分配值“add_item”或“update_item”。 我也尝试使用 javascript 代码,但发现它给出的错误与客户端技术相同。 谁能告诉我我的问题出在哪里?

【问题讨论】:

    标签: forms spring-mvc jstl modelattribute


    【解决方案1】:

    只需使用EL ternary operator。在你的情况下,代码可以写成:

    <form:form  method="post" commandName="${(catalogueEntry==null) ? 'add_item' : 'update_item'}">
        ...
    </form:form>
    

    【讨论】:

      猜你喜欢
      • 2014-02-25
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 2019-06-26
      • 2016-08-25
      • 1970-01-01
      相关资源
      最近更新 更多