Using the Struts Template Tags


This section provides information about the following tags:

  • <template:insert> ” Insert a template with passed content

  • <template:get> ” Retrieve template content

  • <template:put> ” Pass template content

This section uses the testTemplate pages at /Taglib/testTemplate[1-3].do . Figure 15.4 shows the result of requesting testTemplate1.do .

Figure 15.4. Test template page at /Taglib/testTemplate1.do .

graphics/15fig04.gif

The source to this page can be found in Listing 15.6.

Listing 15.6 Calling Out to a Template Page ( testTemplate1.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %> <template:insert template="Template.jsp">   <template:put name="title" direct="yes" content="Test Page 1"/>   <template:put name="body" direct="yes" content="testPage2.jsp"/> </template:insert> 

This JSP file illustrates the first way that values can be passed into a template. In this case, both put tags have the direct attribute set to true, which means that Struts treats the passed values as literal strings.

The template itself is found in Listing 15.7.

Listing 15.7 Calling Out to a Template Page ( Template.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="tmp" %> <%-- The following section shows templates. --%> <html:html> <body> <TABLE><TR><TD><tmp:get name="title"/></TD></TR> <TR><TD> <p>This page provides examples of the following Struts TEMPLATE tags:<br> <ul> <li>&lt;tmp:get&gt;</li> <li>&lt;tmp:insert&gt;</li> <li>&lt;tmp:put&gt;</li> </ul> </TD> <TD><tmp:get name="body"/> </TD> </TR> </TABLE> </body> </html:html> 

Basically, wherever a <template:get> tag is used, it looks for the corresponding value to have been passed in using <template:put> in the calling <template:insert> tag.

In the second example, the direct attribute has been left off the second <template:put> tag. That means Struts looks for a Web resource with the name treated as a URI, and the contents are inserted. In this case (Listing 15.8), the requested page is the logic iteration example from the previous chapter. The results of requesting this page are shown in Figure 15.5

Figure 15.5. Test template page at /Taglib/testTemplate2.do .

graphics/15fig05.gif

Listing 15.8 Inserting a Web Resource into a Template ( testTemplate2.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %> <template:insert template="Template.jsp">   <template:put name="title" direct="yes" content="Test Page 2"/>   <template:put name="body" content="testPage2.jsp"/> </template:insert> 

Finally, you can include content inside the body of a <template:put> tag instead of using the content attribute. If you do so, direct is automatically assumed to be true. Listing 15.9 shows this use of the tag, and Figure 15.6 shows what the page looks like.

Figure 15.6. Test template page at /Taglib/testTemplate3.do .

graphics/15fig06.gif

Listing 15.9 Inserting put Body Content into a Template ( testTemplate3.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %> <template:insert template="Template.jsp">   <template:put name="title" direct="yes" content="Test Page 1"/>   <template:put name="body"><B>This is a test Body</B></template:put> </template:insert> 


Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net