balboa_21
balboa_21

Reputation: 384

Apache Tiles 2.2.2 Pass value from tiles-config.xml to JSP

I have a common header in all the pages except for some pages where the right hand side of header which has some options. For eg, right hand side of my header will have option like : Block, Compose, Edit depending on the page. Is is possible to set this headerShowIcons from tiles-config.xml and then use it in rhsHeader.jsp.

Is there any way by which I can pass some value from tiles-config.xml to rhsHeader.jsp and based on that value I want to show the option(ie, Block,Edit or delete)

Example: if I want to show Compose option to user I just pass value(ie, headerShowIcons='compose') from tiles-config.xml and use it in JSP. If I want to show Block i will pass headerShowIcons='block' which will be read by rhsHeader.jsp to give display Block option. I hope this will be more clear

tiles-config.xml

<definition name="*/*/*/*/*/index" template="/{1}/{2}/xyz/{4}/layouts/layout.jsp">
    <put-attribute name="lhsHeader" value="/{1}/{2}/xyz/s/headerWithBack.jsp" />
    <put-attribute name="rhsHeader" value="/{1}/{2}/xyz/s/rhsHeader.jsp"/>
    <put-attribute name="body" value="/{1}/{2}/xyz/s/myProfile.jsp" />
</definition>

layout.jsp

<!-- What should I add here to meet the requirement -->  
 <body>
   <header> 
      <div>
        <tiles:insertAttribute name="header"/>
        <tiles:insertAttribute name="rhsHeader"/>   
       </div>
   </header>
    <div>
       <tiles:insertAttribute name="body"/>
   </div>
  </body>

rhsHeader.jsp is something like this :

 <tiles:useAttribute name="headerShowIcons" />
 <div class="RHS">
<ul>
<!-- I want to show the options (like- Block,Delete,etc) which will be passed from tiles-config.xml to rhsHeader.jsp (which here I have written it as headerShowIcons )-->
    <c:choose>
        <c:when test="${headerShowIcons  eq 'refine'}">
            <li><a href="#" class="refine">Refine</a></li>
        </c:when>
        <c:when test="${headerShowIcons  eq 'block'}">
            <li><a href="#" class="block">Block</a></li>
        </c:when>
        <c:when test="${headerShowIcons  eq 'edit'}">
            <li><a href="javaScript:void(0);" class="edit">Edit</a></li>
        </c:when>
        <c:when test="${headerShowIcons  eq 'delete'}">
            <li><a href="javaScript:void(0);" class="delete">Delete</a></li>
        </c:when>
        <c:otherwise>
                 <p>test</p>
        </c:otherwise>
    </c:choose>
</ul>

Upvotes: 1

Views: 768

Answers (2)

balboa_21
balboa_21

Reputation: 384

I finally found out the solution to do this, I believe it might helpful to someone with this kind of requirement :

we will have to use

in headerLayout.jsp

<tiles:useAttribute name="whatToShow"/>
  <header> 
    <div>
      <tiles:insertAttribute name="lhsHeader" />
      <tiles:insertAttribute name="rhsHeader">
          <tiles:putAttribute name="whatToShow">${whatToShow}</tiles:putAttribute> 
        </tiles:insertAttribute>
    </div>
   </header>
 <div>
     <tiles:insertAttribute name="body"/>
 </div>

In tiles-config.jsp

 <definition name="Header.*" template="/{1}/layouts/headerLayout.jsp">
    <put-attribute name="lhsHeader" value="/s/headerWithBack.jsp"/>
    <put-attribute name="rhsHeader" value="/s/rhsHeader.jsp"/>  
    <put-attribute name="body" value=""/>
    <put-attribute name="whatToShow" value="block" type="string"/>
</definition>
<!-- inside the definition under whatToShow, I just write whatever value(block,edit,delete,etc) I want to pass to the rhsHeader.jsp -->

in rhsHeader.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<tiles:useAttribute name="whatToShow"/>
<div class="RHS">
<ul>
    <c:choose>
        <c:when test="${whatToShow  eq 'refine'}">
            <li><a href="#" class="refine">Refine</a></li>
        </c:when>
        <c:when test="${whatToShow  == 'block'}">
            <li><a href="#" class="block">Block</a></li>
        </c:when>
        <c:when test="${whatToShow  eq 'edit'}">
            <li><a href="javaScript:void(0);" class="edit">Edit</a></li>
        </c:when>
        <c:when test="${whatToShow  eq 'delete'}">
            <li><a href="javaScript:void(0);" class="delete">Delete</a></li>
        </c:when>
        <c:otherwise>
                 <p></p>
        </c:otherwise>
    </c:choose>
  </ul>
</div>

PS : I have used whatToShow instead of headerShowIcons

Upvotes: 1

QGA
QGA

Reputation: 3192

I would chose a different way of doing it, but if it is what you want please see below

tiles-config.xml

<tiles-definitions>
    <definition name="app.base" template="/WEB-INF/views/default/default.jsp">
        <put-attribute name="title" value="Not Found" />
        <put-attribute name="headerShowIcons" value="" />
        <put-attribute ..........>  
    </definition>
    <definition name="home" extends="app.base">
        <put-attribute name="title" value="Home Page" />
        <put-attribute name="headerShowIcons" value="edit" />   
        <put-attribute ..........>  
    </definition>
</tiles-definitions>

home.jsp

<c:set var="var"><tiles:insertAttribute name="headerShowIcons"/></c:set>

<c:choose>
    <c:when test="${'refine' == var}">
          <li><a href="#" class="refine">Refine</a></li>
    </c:when>
    <c:when test="${'block' == var}">
        <li><a href="#" class="block">Block</a></li>
    </c:when>
    <c:when test="${'edit' == var}">
        <li><a href="javaScript:void(0);" class="edit">Edit</a></li>
    </c:when>
    <c:when test="${'delete' == var}">
        <li><a href="javaScript:void(0);" class="delete">Delete</a></li>
    </c:when>
    <c:otherwise>
         <p>test</p>
    </c:otherwise>
</c:choose>

if you wish you can move this into layout.jsp and show your headers accordingly

Upvotes: 0

Related Questions