Using Struts Bean Tags That Access Java Resources


This section provides information about the following tags:

  • <bean:message> ” Displays an internationalized message

  • <bean:resource> ” Loads a Web application resource as a bean

  • <bean:struts> ” Accesses the internal configuration of Struts

  • <bean:include> ” Gets the response from a Web application request

This section uses the Bean Resources page at /StrutsTaglibs/BeanResources.do . The rendered page is shown in Figure 13.3.

Figure 13.3. The Bean Resources page.

graphics/13fig03.gif

This page demonstrates a typical use of each of the tags. The source is shown in Listing 13.2.

Listing 13.2 JSP File Demonstrating Use of Struts Bean Tags Accessing Java Resources ( BeanResources.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ page import="ch13.RandomQuote" %> <%@ page import="java.util.PropertyResourceBundle" %> <%@ page import="org.apache.struts.action.Action" %> <%@ page import="org.apache.struts.util.MessageResources" %> <html:html> <head> <title>Bean Resources sample code</title> </head> <body bgcolor="white"> <h3>Bean Resources sample code</h3> <p>This page provides examples of the following Struts BEAN tags:<br> <ul> <li>&lt;bean:message&gt;</li> <li>&lt;bean:resource&gt;</li> <li>&lt;bean:struts&gt;</li> </ul> <table border="1" width="100%">   <%--   The following section contains code getting messages.   --%>   <tr>     <td align="left" colspan="2">      <% RandomQuote rq = new RandomQuote();      request.setAttribute("rq", rq);      MessageResources python =         MessageResources.getMessageResources("ch13.pythonquotes");      MessageResources normal =         MessageResources.getMessageResources("ch13.ApplicationResources");      session.getServletContext().setAttribute("ch13.pythonquotes", python);      session.getServletContext().setAttribute(Action.MESSAGES_KEY, normal); %>      <bean:message bundle="ch13.pythonquotes" key="ni"/><P>      <bean:message bundle="ch13.pythonquotes" name="rq"                    property="randomQuoteName"/><P>      <bean:message key="application.uses.struts" arg0="Joe User" arg1="4"/>     </td>   </tr>   <%--   The following section shows how to use web resources.   --%>   <tr>     <td align="left" colspan="2"> Here's the struts-config.xml file for this application:<P> <bean:resource id="resource" name="/testPage1.jsp"/> <bean:write name="resource"/> </td> </tr>   </tr>   <%--   The following section shows how to get a Struts configuration object.   --%>   <tr>     <td align="left" colspan="2"> The type of the /HtmlFile mapping is: <bean:struts id="map" mapping="/HtmlFile"/> <bean:write name="map" property="type"/> </td>   </tr>   <%--   The following section shows how to get an application web response.   --%>   <tr>     <td align="left"> testPage1:<BR> <bean:include id="tp1" page="/testPage1.jsp"/> <bean:write name="tp1" filter="false"/><P> </td><td align="left"> testPage2<BR> <bean:include id="tp2" forward="testpage2"/> <bean:write name="tp2" filter="false"/><P> </td> </tr> </table> </body> </html:html> 

The <bean:message> Tag

The <bean:message> tag is probably the second-most used tag in the Bean taglib after <bean:write> . It is also relatively simple in nature. You provide a message key by using either the key attribute or the name (and possibly property ) attribute. The corresponding string is retrieved from the locale-specific message resource and displayed. If the bundle attribute is used, it specifies the MessageResource bundle that is available under that name on the servlet's context. The arg[0-4] attributes are used to insert values into the message where the corresponding placeholders ( {0} , {1} , and so on) are located. You also can specify a particular locale using the attribute of that name, and the scope of the bean to find using the name attribute.

The examples show how to retrieve a message from an explicit bundle, from an explicit bundle using name and property , and from the default bundle with arguments.

The RandomQuote class simply returns a random key from the Monty Python quote file. The source is shown in Listing 13.3, the Python message file is shown in Listing 13.4, and the default message file is shown in Listing 13.5.

Listing 13.3 Java Class That Returns a Random Quote Key ( RandomQuote.java )
 package ch13; import java.util.Random; public class RandomQuote {   String quoteStrings[] = {"ni", "lake", "chair"};   public String getRandomQuoteName() {     Random r = new Random();     int i = r.nextInt(quoteStrings.length);     return quoteStrings[i];   } } 
Listing 13.4 Monty Python Quote File ( pythonquotes.properties )
 #Silly Monty Python Quotes ni=We are the Knights That Say...Ni! lake=Listen, strange women lyin' in ponds distributin' swords is no basis for \ a system of government! Supreme executive power derives from a mandate from \ the masses, not from some farcical aquatic ceremony! chair=Bring me... the comfy chair! 
Listing 13.5 Default Quote File ( ApplicationResources.properties )
 # Slightly less silly message... but still pretty darn silly. application.totally.lost=I'm sorry, I have no idea how you got here, you \ shouldn't even be seeing this page. In fact, I'll have to kill you now. application.on.486=You dare run an application as great as this on a 486?!! application.uses.struts=I heard that {0} has used Struts for {1} years. \ Struts RULES! Rock on! 

The <bean:resource> Tag

If you ever need to have the contents of a Web resource (which is to say, a file inside the current Web application) available in a local variable, you can use the <bean:resource> tag. It takes two required attributes: id (which is the name of the variable) and name (which is the application-relative path to the file). If you supply any value for the optional input attribute, the id variable is given an InputStream pointing at the file rather than a String containing its contents.

In the example, the tag is used to retrieve the struts-config.xml file and display it. A more typical case might be to retrieve the contents of an XML file so that it can be handed off to a parser for processing.

The <bean:struts> Tag

In the unlikely event that you want to dive into your Struts configuration from JSP, the <bean:struts> tag enables you to gain access to values from the struts-config.xml file. As usual, id is the variable to hold the results. You have your choice of three attributes to use: formBean , forward , or mapping . You must choose one and only one of these, and set it to the name of a form bean, global forward, or action mapping (the path), respectively. The object returned is described in the Javadoc for Struts (respectively, ActionFormBean , ActionForward , or ActionMapping ).

The sample JSP file gets the /HtmlFile mapping and displays the type of the Action defined for it.

The <bean:include> Tag

The <bean:include> tag operates along the same lines as the <jsp:include> tag, except that the data from the Web request is returned in a local variable rather than being displayed on the page. The variable is specified with the id attribute. You give exactly one of forward , page , or href to specify the page to retrieve based on a global ActionForward , an application-relative page URI, or an absolute URL, respectively.

The sample Web page uses a page and forward version of the tag to retrieve two local JSP files. You should note that unlike the <bean:resource> tag, this tag gets the contents by making a request to the server, which means that the JSP is executed rather than being returned verbatim. Again, a good use of this tag might be to get XML content on a foreign server for local processing.



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