Apache Tiles If/Else

喜你入骨 提交于 2019-12-12 10:55:19

问题


I am wondering if it's possible to have an if/else with Apache Tiles 2 (or JSTL that references a Tiles attribute, that would work to). Basically, I want this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<div>
    <!-- Some stuff here -->
</div>
<tiles:if condition="showSecondDiv == 'true'">
    <div>
        <!-- Some second stuff here -->
    </div>
</tiles:if>

There is <put-attribute name="showSecondDiv" value="true" type="string" /> in the Tiles XML. The motivation is that I want to reuse this JSP in a number of places, some that want to show both divs, others that only want to show one.


回答1:


USING JSTL
example:

     <c:if test="${!empty tilesAdditionalTitle}">
       <fmt:param value="${requestScope[tilesAdditionalTitle]}"  />
     </c:if>



回答2:


You can use Tag importAttribute for if/else condition.

layout.xml

<tiles-definitions>
    <definition name="base" template="/WEB-INF/view/template.jsp">
        <put-attribute name="header" value="/WEB-INF/view/header.jsp" />
        <put-attribute name="footer" value="/WEB-INF/view/footer.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/view/sidebar.jsp" />
        <put-attribute name="showSideBar" value="Y" />
    </definition>   
</tiles-definitions>

template.jsp

<body>
    <t:importAttribute name="showSideBar"/>

    <t:insertAttribute name="header"/><br/>

    <div class="uk-container uk-container-center">
        <div class="uk-grid">

            <c:if test="${showSideBar == 'Y'}">
                <div class="uk-width-1-3">
                    <t:insertAttribute name="sidebar"/>
                </div>
            </c:if>

            <div class="${showSideBar == 'Y' ? 'uk-width-2-3' : 'uk-width-1-1'}">
                <t:insertAttribute name="body"/>
            </div>
        </div>
    </div>

    <%@include file="/WEB-INF/view/common/loading.jsp"%>

    <t:insertAttribute name="footer"/>

</body>

If you do not want to show, then set <put-attribute name="showSideBar" value=""/> in the layout.xml file.



来源:https://stackoverflow.com/questions/7028014/apache-tiles-if-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!