This section provides information on the following tags:
This section uses the Bean Bean page at /StrutsTaglibs/BeanBean.do . The rendered page is shown in Figure 13.4. Figure 13.4. The Bean Bean page.
This page demonstrates a typical use of each of the tags. The source is shown in Listing 13.6. Listing 13.6 JSP File Demonstrating Use of Struts Bean Tags Accessing Bean Properties ( BeanBean.jsp )<%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ page import="java.util.HashMap" %> <%@ page import="java.util.GregorianCalendar" %> <%@ page import="java.util.Calendar" %> <html:html> <head> <title>Bean Bean sample code</title> </head> <body bgcolor="white"> <h3>Bean Bean sample code</h3> <p>This page provides examples of the following Struts BEAN tags:<br> <ul> <li><bean:define></li> <li><bean:write></li> <li><bean:size></li> </ul> <table border="1" width="100%"> <%-- The following section contains code defining variables. --%> <tr> <td align="left" colspan="2"> <bean:define id="name" value="Tagslibs"/> Application Name: <bean:write name="name"/><BR> <% request.setAttribute("session", session); %> <bean:define id="context" name="session" property="servletContext"/> Server Info: <bean:write name="context" property="serverInfo"/><BR> <bean:define id="context_copy" name="context" type="javax.servlet.ServletContext"/> Server Major Version: <bean:write name="context_copy" property="minorVersion"/> </td> </tr> <%-- The following section contains code getting the size of a collection. --%> <tr> <% HashMap lines = new HashMap(); lines.put("1", "Line 1"); lines.put("2", "Line 2"); lines.put("3", "Line 3"); request.setAttribute("lines", lines); %> <td align="left" colspan="2"> <bean:size id="length" name="lines"/> Line Count: <bean:write name="length"/> </td> </tr> <%-- The following section contains code showing how to write bean values. --%> <tr> <% request.setAttribute("floatval", Float.valueOf("3.14159")); Calendar gc = GregorianCalendar.getInstance(); gc.setTime(new java.util.Date(session.getCreationTime())); request.setAttribute("now", gc); String boldStart = "<B>"; String boldEnd = "</B>"; request.setAttribute("bs", boldStart); request.setAttribute("be", boldEnd); %> <td align="left" colspan="2"> Pi is: <bean:write format="#.####" name="floatval"/><BR> Session Started at: <bean:write format="MM-dd-yyyy hh:mm:ss" name="now" property="time"/><BR> This should not be in <bean:write name="bs"/>bold <bean:write name="be"/><BR> This should be in <bean:write name="bs" filter="false"/>bold <bean:write name="be" filter="false"/><BR> </td> </tr> </table> </body> </html:html> The <bean:define> TagIf you need to define a new bean in a given scope, you can do so using the <bean:define> tag. This tag can work in three ways. First, it can create a new attribute set to a string value. Second, it can create a new attribute set to an old attribute's value. Finally, it can create a new attribute from a property of an old attribute. Again, id is used to tell Struts what the name of the new attribute will be. If you set value equal to a string, the value will be that string. If you set name equal to an existing bean name, the value will be the value of that bean. If you use both name and property , the value will be that property of that bean. You can use scope to specify the scope of the bean that is used as the source (defaults to page context), and toScope to specify the scope of the new bean. If you're setting one bean directly to another or to a property of that bean, you can use the type attribute to tell Struts what the type of the value being set is.
The example shows three variants: setting a bean to a property of another bean, setting a bean to a string, and setting a bean to another bean. The <bean:size> TagThe <bean:size> tag enables you to set a variable to the size of a map, collection, or array. You can use name , name combined with property , or collection (which should be a run-time expression that returns an array, collection, or map). As usual, id holds the value and scope enables you to specify the scope of the bean named by the name attribute. The example shows a simple example of getting the size of a HashMap . The <bean:write> TagIn my experience, this is the most commonly used tag in the entire Struts taglib package. Its purpose is to display a bean or bean property, and possibly format it. The simplest use cases merely specify name or name and property . In those cases, the value is written to the output stream using default formatting for numbers , dates, and other non-string values. The scope attribute, as always, specifies where to look for the bean. If you want to specify formatting, you can do it in one of two different ways. You can use the format attribute and an appropriate format string for the type ( ###.### for a number, for example). Alternatively, you can use the formatKey attribute to specify a key into a resource bundle, which is used to retrieve the formatting string. If you want to use a bundle other than the default, the bundle attribute allows that. The locale value enables you to define the locale that will be used both for finding a bundle and for locale-specific formatting. If the bean isn't found, an exception is thrown. However, if you set ignore to true, the <bean:write> will simply be ignored instead. Finally, the value sent to the output stream is normally enclosed in quotation marks so that the HTML instructions won't be processed . If you want the output to be sent raw, set filter to false. The examples show printing a float in a bean with formatting, printing a date that is a property of a bean with formatting, and displaying HTML directives both with and without filtering. |