Solved

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

Posted on 2004-09-09
4
6,555 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now