Solved

Struts Tiles - Passing a parameter from layout template into a tile JSP?

Posted on 2004-09-09
4
6,614 Views
Last Modified: 2008-01-09
Experts - I am creating a web app using Tomcat v4.1, JDK v1.4.2, and Struts v1.1 (with Tiles).  The format/layout of each JSP in our web app is controlled by a Tiles layout template (call it Layout.jsp).  Layout.jsp in turn "inserts" or uses Header.jsp as one of it's tiles to display a common header on each page.  Also contained in Header.jsp is some code for a navigation menu at the top of the header/page ("i.e like -> Main | Download | Search | Help | Contact Us").  

What I want to be able to do is somehow pass a parameter into Header.jsp that indicates which page I am currently on, so that I can disable that page link, and enable all of the other page links.  

So to try and simplify this: I have a content JSP called Mainmenu.jsp.  I want to setup a parameter in Mainmenu.jsp, that gets passed through Layout.jsp, and into Header.jsp so that I can disable just the "Main" link, and enable the remaining page links.  I have looked over tiles tags but I am not sure how to go about doing this.  Or maybe there is a better way to approach this all together ?  Any help/ideas would be MOST appreciated!

Hopefully this makes sense - Thanks In Advance!

Mark


RELEVENT CODE SNIPPETS:

LAYOUT.JSP
--------------
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<html:html>
<head>
<title><tiles:getAsString name="pagetitle" /></title>
<tiles:insert attribute="pageheader"/>
</head>
...

MAINMENU.JSP
-----------------
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

<tiles:insert page="/Layout.jsp" flush="true">

<tiles:put name="pagetitle" type="string"> <bean:message key="title.mainmenu" /> </tiles:put>

***** WANT TO PASS PARAMETER INTO THIS TILE! *****
<tiles:put name="pageheader" value="/Header.jsp" />

HEADER.JSP  
--------------
***** WHAT STATEMENT(s) WOULD I NEED TO GET THE PARAMETER TO PROPERLY SETUP THE LINKS ? *****
0
Comment
Question by:mbeede
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
boonleng earned 500 total points
ID: 12023657
You can try using the following codes:
Layout.jsp
...
<tiles:insert attribute="pageheader">
  <tiles:put name="currentPage"><tiles:getAsString name="currentPage"/></tiles:put>
</tiles:insert>

MainMenu.jsp
...
<tiles:insert page="/Layout.jsp"  flush="true">
  <tiles:put name="pageheader" value="/Header.jsp"/>
  <tiles:put name="currentPage" value="welcome.do"/>
</tiles:insert>

Header.jsp
<tiles:importAttribute />
<logic:notEqual name="currentPage" value="welcome.do">
  <html:link forward="welcome">Welcome</html:link>
</logic:notEqual>


Another better way of doing this is to define the layout, menu...etc in tiles-defs.xml then overide the menu definition for specific page. You can refer to this file on how to setup tiles-defs.xml and menu http://www.lifl.fr/~dumoulin/tiles/tilesAdvancedFeatures.pdf

Example: tiles-defs.xml

   <definition name=".global.layout"                               path="/ayout.jsp">
      <put name="title"                    value="My Web Site" />
      <put name="header"                    value="/header.jsp" />
      <put name="menu"                    value=".menu.1" />
      <put name="body"                    value="/body.jsp" />
      <put name="footer"                    value="/footer.jsp" />      
    </definition>
   
    <definition name=".menu.1"                               path="/menu.jsp" >
      <put name="title" value="Main Menu" />
      <putList name="items" >
            <item value="SampleForm.title.add"      
                    link="/sample_add.do"
                    classtype="org.apache.struts.tiles.beans.SimpleMenuItem"/>
            <item value="UserGroupsForm.title.list"      
                    link="/usergroups_list.do"
                    classtype="org.apache.struts.tiles.beans.SimpleMenuItem"/>
        </putList>
      </definition>

    <definition name=".menu.2"                               path="/menu.jsp" >
      <put name="title" value="Main Menu" />
      <putList name="items" >
            <item value="com.cv.v2sm.web.SampleForm.title.add"      
                    link="/sample_add.do"
                    classtype="org.apache.struts.tiles.beans.SimpleMenuItem"/>
            <item value="com.cv.v2sm.web.UserGroupsForm.title.list"      
                    link="#"
                    classtype="org.apache.struts.tiles.beans.SimpleMenuItem"/>
        </putList>
      </definition>
 
    <definition name=".sample.add"                                     extends=".global.layout">
      <put name="title"              value="Sample Page" />
      <put name="menu"              value=".menu.1" />
      <put name="body"              value="sample_add.jsp" />
    </definition>

    <definition name=".usergroup.add"                               extends=".global.layout">
      <put name="title"              value="Add User Groups" />
      <put name="menu"              value=".menu.2" />
      <put name="body"              value="usergroup_add.jsp" />
    </definition>

Example menu.jsp
<%@ page language="java" %>
<%@ taglib uri="/tags/struts-html"       prefix="html"%>
<%@ taglib uri="/tags/struts-tiles"      prefix="tiles"%>
<%@ taglib uri="/tags/struts-bean"       prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>

<%@ page import="java.util.Iterator" %>
<tiles:importAttribute />

<table class="menu">
  <logic:present name="title">
  <tr>
    <td colspan=2 class="title"><tiles:getAsString name="title"/></td>
  </tr>
  </logic:present>

  <logic:iterate id="item" name="items" type="org.apache.struts.tiles.beans.MenuItem" >
  <tr>
    <td>
    <logic:equal name="item" parameter="link" value="#">
        <bean:message key="<%=item.getValue()%>"/><br>
      </logic:equal>
    <logic:notEqual name="item" parameter="link" value="#">
        <html:link action="<%=item.getLink()%>"><bean:message key="<%=item.getValue()%>"/></html:link><br>
      </logic:notEqual>
    </td>
  </tr>
  </logic:iterate>

</table>
0
 
LVL 14

Expert Comment

by:boonleng
ID: 12023668
opps...should be 'property' instead of 'parameter' :p

  <logic:iterate id="item" name="items" type="org.apache.struts.tiles.beans.MenuItem" >
  <tr>
    <td>
    <logic:equal name="item" property="link" value="#">
       <bean:message key="<%=item.getValue()%>"/><br>
     </logic:equal>
    <logic:notEqual name="item" property="link" value="#">
       <html:link action="<%=item.getLink()%>"><bean:message key="<%=item.getValue()%>"/></html:link><br>
     </logic:notEqual>
    </td>
  </tr>
  </logic:iterate>
0
 

Author Comment

by:mbeede
ID: 12027662
Boonleng - Thank you for providing the code details.  I went with your first example (since going to XML based definitions would require too much rework at this time).  I plugged in the code exactly as you specified.  But I am getting the following run-time error now when I try to display the first JSP page:

[ServletException in:/pageLayout.jsp] Error - tag.getAsString : attribute 'pageactive' not found in context. Check tag syntax' javax.servlet.jsp.JspException: Error - tag.getAsString : attribute 'pageactive' not found in context. Check tag syntax at org.apache.struts.taglib.tiles.GetAttributeTag.doEndTag(GetAttributeTag.java:204) at org.apache.jsp.pageLayout_jsp._jspx_meth_tiles_getAsString_1(pageLayout_jsp.java:278) at org.apache.jsp.pageLayout_jsp._jspx_meth_tiles_put_0(pageLayout_jsp.java:253) at org.apache.jsp.pageLayout_jsp._jspx_meth_tiles_insert_0(pageLayout_jsp.java:220) at

NOTE: I am using the parameter name "pageactive" instead of your name "currentPage".  I will keep trying to fix this, but do you know what may be causing this?

Thanks Again!
0
 

Author Comment

by:mbeede
ID: 12028105
Boonleng - Found the run-time error.  It was my mistake.  Ignore my previous reply.  Thanks again for all of the details :)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with Pie chart in Jsp 20 215
java beans and EJBs 5 192
java cigar challenge 58 210
approach on java challenges Approach on java challenges or real world challenges 29 160
How do we balance the user experience (UX) with reasonable security measures? It can be done, if you keep these fundamentals in mind.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question