Solved

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

Posted on 2004-09-09
4
6,844 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 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