Apache Tiles If/Else
Asked Answered
D

2

6

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.

Danielladanielle answered 11/8, 2011 at 14:51 Comment(0)
G
4

USING JSTL
example:

     <c:if test="${!empty tilesAdditionalTitle}">
       <fmt:param value="${requestScope[tilesAdditionalTitle]}"  />
     </c:if>
Gerontology answered 11/8, 2011 at 15:9 Comment(3)
Will that reference the attribute declared in my Tiles XML?Danielladanielle
tilesAdditionalTitle this is the name of attributeGerontology
Ok, and what is the requestScope[] part?Danielladanielle
P
1

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.

Poore answered 24/10, 2017 at 4:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.