Chapters 6 and 7 discussed how to access scoped objects from JSPs using the Expression Language (EL) and JSTL. The Bean and Logic Tag Libraries are Struts’ own tag libraries for achieving practically the same thing. This book and the Struts team recommend that you use the EL and JSTL, instead of the Bean and Logic Tag Libraries. In the face of it, Bean and Logic are discussed in detail in this book because both are still widely used and will still continue to be used for a while. In addition, those planning to migrate from these libraries to JSTL will need a guide. This chapter discusses the Bean Tag Library and Chapter 9 the Logic Tag Library.
Note
Use EL and JSTL instead of the Bean and Logic Tag Libraries for the following reasons:
The list of tags in the Bean Tag Library is presented in Table 8.1.
| Tag | Description | JSTL Equivalent |
|---|---|---|
| cookie | Creates a scoped variable that references a cookie. | |
| define | Creates a scoped variable that references a scoped object or a scoped object’s property, or creates a new scoped object and a scoped variable that references it. | c:set |
| header | Retrieves a request header and creates a scoped variable that references the header. | c:set |
| include | Retrieves an external or internal resource and stores the content in a scoped object. | c:import |
| message | Displays an internationalized message. | c:out |
| page | Creates a scoped variable that references one of the following JSP implicit objects: request, response, session, application, config. | c:set |
| parameter | Creates a scoped variable that references a request parameter. | c:set |
| resource | Retrieves the contents of a Web resource, stores them as a String, and creates a scoped variable that references the String. | c:import |
| size | Creates a scoped object that contains the number of elements in a collection. | The length function in JSTL 1.1 |
| struts | Creates a scoped variable that references a Struts configuration object. | |
| write | Prints the value of a scoped object or a scoped object’s property | c:out |
Table 8.1: The Bean Tag Library’s Tags
Note
To reference the Bean Tag Library’s tags indirectly, you need the struts-bean.tld file in your WEB-INF directory and the following taglib element in your web.xml file.
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
Most of the tags in the Bean Tag Library were designed to access scoped objects and various objects in the HttpServletRequest object. Using these tags you can access cookies, request headers, request parameters, etc. For example, the cookie tag is used for reading cookies, the header tag for reading request headers, and so forth. These tags allow you to create a scoped variable that can then be used with the write tag to display the value in the browser. Therefore, these tags normally work in two steps.
In other words, there is no tag similar to the JSTL out tag that can read a value and display it at the same time.
The tags in Struts’ core tag libraries do not use the Expression Language. Instead, Struts introduces three types of references you can use to access a scoped object or a scoped object’s property: simple, nested, and indexed.
Simple references are used to access a scoped object’s property, similar to the standard <jsp:getProperty> and <jsp:setProperty> tags. A reference to a property named foo is converted into a method call getFoo or setFoo, as appropriate, using the standard JavaBeans Specification naming conventions for bean properties.
Nested references allow you to access a property of a property. A dot is used to separate between properties. For example, city.address refers to the address property of the object returned by the city property.
Indexed references are used to access individual elements of a property. For example, foo[0] refers to the first member of the collection referenced by the foo property.
In practice, the nested and indexed properties can be combined in any way. For example, foo[2].bar refers to the bar property of the third member of the collection referenced by foo.
Each of the tags in the Bean Tag Library is given in a separate section in this chapter.
Note
The app08a application presents the examples of the tags in the Bean Tag Library. You can run it by using the following URL.
http://localhost:8080/app08a/testBean.do