Home

This Chapter
-Chapter 8: The Bean Tag Library
-The write Tag
-The cookie Tag
-The header Tag
-The parameter Tag
-The define Tag
-The page Tag
-The include Tag
-The message Tag
-The resource Tag
-The size Tag
-The struts Tag
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Chapter 8

The Bean Tag Library

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:

  1. The EL and JSTL are more powerful and more comprehensive than the Bean and Logic Tag Libraries.
  2. Since JSTL is a standard, chances are most JSP programmers are more familiar with JSTL than with Struts tag libraries.
  3. The HTML Tag Library is Struts specific and has no equivalent in JSTL. Therefore, it will still be used in Struts applications.

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.

  1. Read a value (using cookie, header, parameter, etc) and create a scoped variable that references the value.
  2. Use the write tag to displays the value of the scoped variable created.

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.

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

Previous
Next