This section provides information about the following tags:
<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.
This page demonstrates a typical use of each of the tags. The source is shown in Listing 13.2.
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.
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]; } } |
#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! |
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.
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 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.