【问题标题】:Can jstl check if attribute has been added to the model?jstl可以检查属性是否已添加到模型中吗?
【发布时间】:2011-05-19 01:39:40
【问题描述】:

是否可以检查某个属性是否已添加到模型中?

//in the controller teh variable is not always added
//
model.addAttribute("variable", myVariable);

在jsp中是这样的

<c:choose>
    <c:when test="${variable is present}">
        Not present
    </c:when>
    <c:otherwise>
        Present
    </c:otherwise>
</c:choose>

谢谢

【问题讨论】:

    标签: java jsp servlets jstl el


    【解决方案1】:

    可以检查属性是否为空

    <c:if test="${not empty post}">
       <h3>${post.title}</h3>   
    </c:if>
    

    【讨论】:

      【解决方案2】:

      JSTL/EL 无法检查属性是否已添加到模型中。为此,您需要自己实现一个观察者/可观察者。

      但是,EL 可以检查 bean 属性或映射值是否不为 null 或为空。

      <c:when test="${not empty bean.property}">
      

      <c:when test="${not empty map.key}">
      

      另见:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-08
        • 2019-10-12
        • 1970-01-01
        • 2013-06-14
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        相关资源
        最近更新 更多