Solved

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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