【问题标题】:Render HTML conditionally in Spring MVC在 Spring MVC 中有条件地渲染 HTML
【发布时间】:2012-06-04 15:21:14
【问题描述】:

是否有任何标签可以有条件地呈现 HTML 块。例如:Struts 有:

<logic:present name="someForm" property="someProperty">
    //Code block
</logic:present>

例如:JSF 有:

<h:panelGrid rendered="#{not empty someList}">
    //Some code block
</h:panelGrid>

Spring MVC 中有类似的东西吗?

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    你可以使用通用的 JSP/JSTL 标签库

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    <c:choose>
            <c:when test="${condition}">
                something
            </c:when>
            <c:otherwise>
                something else
            </c:otherwise>
        </c:choose>
    

    或者

    <c:if test="${condition}">
            something
        </c:if>
    

    使用 c:if 据我所知没有其他条件

    【讨论】:

      【解决方案2】:

      The JSTL:

      <c:if test="${!empty someForm.someProperty}">
      
      </c:if>
      

      【讨论】:

        【解决方案3】:

        简单的旧 JSTL 来拯救你!

        Spring MVC 的美妙之处在于它不像其他框架那样添加大量冗余标签库。您始终可以依赖 JSTL 进行此类检查,现在这是 JSP 规范的一部分。

        <c:if test="${not empty someList}">
        
        </c:if>
        

        【讨论】:

        • 是的,我知道我可以使用 JSTL,但我不知道 spring mvc 本身没有这样的东西,但现在我知道了。谢谢你的回答:)
        猜你喜欢
        • 2022-12-13
        • 1970-01-01
        • 2018-01-24
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 2018-06-30
        • 2014-11-26
        • 2012-11-20
        相关资源
        最近更新 更多