Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-09-09
4
Medium Priority
?
7,171 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The number of companies understanding the potential of IoT on B2B market is growing with each day. And yet only a small share of IoT developers have managed to equalize incomes and stay competitive in the international market.
If you try to migrate from Elastix to Issabel, you will face a lot of issues. These problems are inevitable but fortunately, you can fix them. In the guide below, I will explain how I performed the migration while keeping all data and successfully t…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

564 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