Render HTML conditionally in Spring MVC

2020-03-26 03:38发布

Is there any tag which lets to render the HTML blocks conditionally. For e.g.: Struts has:

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

For e.g.: JSF has:

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

Is there anything like that in spring MVC?

3条回答
Summer. ? 凉城
2楼-- · 2020-03-26 04:08

The JSTL:

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

</c:if>
查看更多
看我几分像从前
3楼-- · 2020-03-26 04:26

you could use the common JSP/JSTL taglibs

<%@ 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>

alternatively

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

using c:if there is no else condition as far as i know

查看更多
家丑人穷心不美
4楼-- · 2020-03-26 04:28

Plain old JSTL to your rescue!

The beauty of Spring MVC is that it doesn't add tons of redundant tag libraries unlike other frameworks. You can always rely on JSTL for such checks which is part of the JSP spec now.

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

</c:if>
查看更多
登录 后发表回答