The Bean Tag Library Tags

 < Day Day Up > 



The following table lists each of the tags in the Bean Tag Library and a short description of each tag’s purpose.

Tag

Description

cookie

Used to store a reference to an HTTP cookie (javax.servlet.http.Cookie) from the incoming request in a JSP scripting variable with page scope.

define

Used to store a reference to a new or existing object in a JSP scripting variable.

header

Used to store an HTTP header from the incoming request as a String object in a JSP scripting variable with page scope.

include

Used to perform a request to a page and store the contents as a String object in a JSP scripting variable with page scope.

message

Used to retrieve an internationalized message from Struts’ ApplicationResources.properties file and render it to a JSP’s output stream.

page

Used to store one of a JSP’s implicit objects in a JSP scripting variable with page scope.

parameter

Used to store a parameter from the incoming request as a String object in a JSP scripting variable with page scope.

resource

Used to load the contents of a Web application resource and store them as a String object in a JSP scripting variable with page scope.

size

Used to store the size (number of elements) of an array, java.util.Collection-based object, or java.util.Map-based object as a java.lang.Integer in a JSP scripting variable with page scope.

struts

Used to store a reference to a Struts configuration object in a JSP scripting variable with page scope.

write

Used to render the value of an object to a JSP’s output stream.

Following is a dedicated section for each tag, including a complete description of the tag, a table listing each of the tag’s attributes, and a usage example for the tag. In the tables that describe each tag’s attributes, pay special attention to the Accepts Scriptlet and Required columns. The Required column simply denotes whether or not the given attribute is required when using the tag. If an attribute is required and you do not specify it when using the tag, the tag will throw a javax.servlet.jsp.JspException at run time. Note that you can declare an error page in your JSP with a page directive to capture any JspExceptions that might be thrown, as shown here:

<%@ page errorPage="error.jsp" %>

If an exception occurs, the page specified by the errorPage attribute will be internally redirected to display an error page.

The Accepts Scriptlet column denotes whether or not the given attribute’s value can be specified with a JSP scriptlet. If a JSP scriptlet is used to specify an attribute value, the scriptlet must comprise the complete value, quote (“) to quote (“), as shown here.

Correct:

<bean:cookie  name="<%=catName%>"/>

Incorrect:

<bean:cookie  name="<%=catName%>-cat"/> 

Notice in the incorrect example that “-cat” is used as part of the value for the name attribute following the scriptlet. This is invalid because there are extra characters between the end of the scriptlet and the ending quote.

A corrected version of the incorrect example follows:

<bean:cookie  name="<%=catName + "-cat"%>"/>

The concatenation of “-cat” is now part of the scriptlet and the scriptlet comprises the complete value for the attribute.

The cookie Tag

The cookie tag is used to store a reference to an HTTP cookie (javax.servlet.http.Cookie) from the incoming request in a JSP scripting variable with page scope. Additionally, the cookie tag can be used to store multiple cookies of the same name if the multiple attribute is specified. For example, if the incoming request has two cookies that are both named “category”, a Cookie[ ] array would be used to capture each cookie value and then the array would be stored in the specified JSP variable.

If the incoming request does not include a cookie with the specified name attribute and the value attribute is not specified, a JspException will be thrown by this tag at run time.

Attributes

Attribute

Description

Accepts Scriptlet

Required

id

Specifies the name of the JSP variable that will hold the specified HTTP cookie(s).

No

Yes

multiple

Accepts an arbitrary value to denote that multiple cookies should be stored. If specified, cookies will be stored in a Cookie[ ] array. If not specified and multiple cookies have the same name, the first cookie with the name specified will be stored.

Yes

No

name

Specifies the name of the HTTP cookie(s) to store in the JSP variable.

Yes

Yes

value

If the cookie specified with the name attribute is not found and this attribute is specified, a new Cookie object will be created and assigned the value of this attribute.

Yes

No

Example Usage

The following snippet shows a basic usage of the cookie tag:

<bean:cookie  name="cat" value="default"/>

This example attempts to find a cookie named “cat” in the incoming request and store a reference to it in a variable named “category”. If the cookie is not in the request, a new cookie will be created and assigned the value of “default”. This new cookie will then have a reference stored to it.

After you have used the cookie tag to store a cookie, you can access it with the following snippet:

Category Cookie: <%=category.getValue()%>

This snippet simply uses a JSP expression to render the value of the Cookie object stored in the category variable. Additionally, because the cookie tag places the category variable in page scope, it can be accessed by other tags, as shown here:

Category Cookie: <bean:write name="category" property="value"/>

The following snippet shows an example of using the cookie tag for multiple cookies with the same name:

<bean:cookie  name="cat" multiple="true"/>

The multiple attribute accepts arbitrary values; thus, you could use “true” as shown in the example, or any other value. As long as a value is present, a Cookie[ ] array will be used to store the cookies. To access the first cookie stored when using the multiple attribute, use the following snippet:

Category Cookie: <%=category[0].getValue()%>

The define Tag

The define tag is used to store a reference to an existing object in a JSP scripting variable. This tag can also be used to create a new object (of type String), whose reference will be stored in a JSP scripting variable. Additionally, the define tag allows you to specify what scope the object reference should be placed in.

There are four ways you can use the define tag, as listed here and shown later in the “Example Usage” section:

  • You can use the name attribute to specify the name of the object to which a reference will be stored. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object to which a reference will be stored. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the value attribute to specify a value that will be used to create a new String object whose reference will be stored.

  • You can place text between opening and closing define tags that will be used to create a new String object whose reference will be stored.

If none of the aforementioned ways is used to specify the object whose reference should be stored, a JspException will be thrown by the tag at run time.

You may have noticed that this tag is similar to JSP’s “built-in” <jsp:useBean> tag; however, it differs markedly, as described in this list:

  • The define tag unconditionally creates (or replaces) a scripting variable with the specified id attribute.

  • The define tag can create a scripting variable from one of an object’s fields, as specified with the property attribute.

  • The define tag can create a scripting variable from a literal string with use of the value attribute.

  • The define tag does not support nested <jsp:setProperty> tags.

Attributes

Attribute

Description

Accepts Scriptlet

Required

id

Specifies the name of the JSP variable that will hold the specified object reference.

No

Yes

name

Specifies the name of the object to which a reference will be stored. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return the object to which a reference will be stored.

Yes

No

property

Specifies the field of the object specified by the name attribute, whose getter method will be called to return the object to which a reference will be stored.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

toScope

Specifies the scope (application, page, request, or session) that the specified object reference should be stored in.

Yes

No

type

Specifies the fully qualified class type (e.g., java.lang.Integer) of the object being stored as a scripting variable. If not specified, defaults to java.lang.String if you specify a value attribute; otherwise, defaults to java.lang.Object.

Yes

No

value

Specifies a value that will be used to create a new String object whose reference will be stored.

Yes

No

Example Usage

As mentioned, there are four ways you can use the define tag. The first way, shown here, uses the name attribute to specify the name of the object to which a reference will be stored:

<bean:define  name="nameObj"/>

Of course, this example assumes you have an object in some scope with the name “nameObj”. Remember that you can explicitly specify the scope of the object with the scope attribute.

The second way to use the define tag is shown here:

<bean:define  name="nameObj" property="nameField"/>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object to which a reference will be stored. Again, you can explicitly specify the scope of the object with the scope attribute.

The third way to use the define tag is shown here:

<bean:define  value="James"/>

In this example, the value attribute is used to specify the value for a new String object whose reference will be stored.

The following is the fourth and final way to use the define tag:

<bean:define >James</bean:define>

This example uses the nested string between the opening and closing define tags to specify a value for a new String object whose reference will be stored.

After you have used the define tag to store a reference to an object, you can access it with the following snippet:

Name: <%=name%>

This snippet simply uses a JSP expression to render the value of the object stored in the name variable. Additionally, because the define tag places the name variable into a scope, it can be accessed by other tags, as shown here:

Name: <bean:write name="name"/>

The header Tag

The header tag is used to store a reference to an HTTP header from the incoming request as a String object in a JSP scripting variable with page scope. Additionally, the header tag can be used to store multiple headers of the same name if the multiple attribute is specified. For example, if the incoming request has two headers that are both named “Accept-Language”, a String[ ] array would be used to capture each header value and then the array would be stored in the specified JSP variable.

For more information on the HTTP protocol and its headers, visit the World Wide Web Consortium (W3C) page located at http://www.w3.org/Protocols/HTTP/.

If the incoming request does not include a header with the specified name attribute and the value attribute is not specified, a JspException will be thrown by this tag at run time.

Attributes

Attribute

Description

Accepts Scriptlet

Required

id

Specifies the name of the JSP variable that will hold the specified HTTP header(s).

No

Yes

multiple

Accepts an arbitrary value to denote that multiple headers should be stored. If specified, headers will be stored in a String[ ] array. If not specified and multiple headers have the same name, the first header with the name specified will be stored.

Yes

No

name

Specifies the name of the HTTP header(s) to store in the JSP variable.

Yes

Yes

value

Specifies the default value to assign to the String object being placed in the JSP variable if the specified header is not present in the incoming request.

Yes

No

Example Usage

The following snippet shows a basic usage of the header tag:

<bean:header  name="User-Agent" value="unknown"/>

This example attempts to find an HTTP header named “User-Agent” in the incoming request and store a reference to it in a variable named “browser”. If the header is not in the request, a new String object will be created and have the value of the value attribute assigned to it. A reference to the String will then be stored.

After you have used the header tag to store a header, you can access it with the following snippet:

This page was requested using: <%=browser%>

This snippet simply uses a JSP expression to render the value of the browser variable. Additionally, because the header tag places the browser variable in page scope, it can be accessed by other tags, as shown here:

This page was requested using: <bean:write name="browser"/> 

The following snippet shows an example of using the header tag for multiple headers with the same name:

<bean:header  name="Accept-Language" multiple="true"/>

Remember that the multiple attribute accepts an arbitrary value; thus, you could use “true” as shown in the example, or any other value. As long as a value is present, a String[ ] array will be used to store the headers. To access the first header stored when using the multiple attribute, use the following snippet:

First Language: <%=languages[0]%>

The include Tag

The include tag is used to perform a request to a page and store the contents as a String object in a JSP scripting variable with page scope. This tag is similar to JSP’s built-in <jsp:include> tag; however, it differs because it stores the contents of the included page in a scripting variable instead of writing the contents to the JSP output.

There are three ways you can use the include tag, as listed here and shown later in the “Example Usage” section:

  • You can use the forward attribute to specify the name of a forward, from the Global Forwards Configuration section of the Struts configuration file, which contains the URL of the page whose contents should be stored.

  • You can use the href attribute to specify the absolute URL of the page whose contents should be stored.

  • You can use the page attribute to specify an application-relative URL whose contents should be stored.

Attributes

Attribute

Description

Accepts Scriptlet

Required

anchor

Specifies the anchor (e.g., “#bottom”) to be added to the URL for this include. This value should be specified without the leading hash (#) character.

Yes

No

forward

Specifies the name of a forward, from the Global Forwards Configuration section of the Struts configuration file, which contains the URL of the page whose contents should be stored.

Yes

No

href

Specifies the absolute URL, including the protocol, of the page whose contents should be stored (e.g., http://www.yahoo.com).

Yes

No

id

Specifies the name of the JSP variable that will hold the contents of the specified page.

No

Yes

name

DeprecatedUse the page attribute instead.

Yes

No

page

Specifies the application-relative URL (starts with a leading slash, /) of the page whose contents should be stored.

Yes

No

transaction

Accepts “true” or “false” to denote whether or not you want the current transaction token included in the URL for this include.

Yes

No

Example Usage

As mentioned, there are three ways you can use the include tag. The first way, shown here, uses the forward attribute to specify the name of a forward, from the Global Forwards Configuration section of the Struts configuration file, whose contents will be stored:

<bean:include  forward="search"/>

The following is the second way to use the include tag:

<bean:include  href="http://www.yahoo.com/"/>

This example uses the href attribute to specify the absolute URL of a page whose contents will be stored.

The third way to use the include tag is shown here:

<bean:include  page="/search.jsp"/>

In this example, the page attribute is used to specify an application-relative URL whose contents will be stored.

The message Tag

The message tag is used to retrieve an internationalized message from Struts’ ApplicationResources.properties file and render it to a JSP’s output stream. Remember that Struts’ ApplicationResources.properties file is based on Java’s Resource Bundle functionality for externalizing and internationalizing application strings, messages, and labels. Following is a sample ApplicationResources.properties file:

# Label Resources label.search.name=Name label.search.ssNum=Social Security Number # Error Resources error.search.criteria.missing=<li>Search Criteria Missing</li> error.search.ssNum.invalid=<li>Invalid Social Security Number</li> errors.header=<font color="red"><b>Validation Error(s)</b></font><ul> errors.footer=</ul><hr width="100%" size="1" noshade="true">

As you can see, the file is made up of key/value pairs. The message tag simply looks up a message with a given key and returns its matching value.

The message tag also allows you to insert content into messages at run time before they are rendered using parametric replacement, which is the substitution of an argument for a placeholder parameter at run time. Following is an example message that uses parametric replacement:

missing.field=The {0} field is missing.

Notice the {0} reference in the message. At run time, the {0} parameter will be replaced with another value. The message tag allows you to specify what the replacement values are by use of the arg0arg4 attributes. Thus, you can have up to five parametric replacements in a message with {0} – {4}.

There are three ways you can use the message tag, as listed here and shown later in the “Example Usage” section:

  • You can specify a message’s key with the key attribute.

  • You can use the name attribute to specify the name of the object whose value will be used as the key for the message. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used as the key for the message. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

If the specified message does not exist, a JspException will be thrown by this tag at run time.

Attributes

Attribute

Description

Accepts Scriptlet

Required

arg0

First parametric replacement value as specified by {0} in the message to be rendered.

Yes

No

arg1

Second parametric replacement value as specified by {1} in the message to be rendered.

Yes

No

arg2

Third parametric replacement value as specified by {2} in the message to be rendered.

Yes

No

arg3

Fourth parametric replacement value as specified by {3} in the message to be rendered.

Yes

No

arg4

Fifth parametric replacement value as specified by {4} in the message to be rendered.

Yes

No

bundle

Specifies the name of the application scoped org.apache.struts.util.MessageResources object that contains the messages to be rendered. Defaults to the value stored in the org.apache.struts.Globals.MESSAGES_KEY constant.

Yes

No

key

Specifies the name of the key whose corresponding value will be rendered.

Yes

No

locale

Specifies the name of the session scoped java.util.Locale object to use for selecting which message to render. Defaults to the value stored in the org.apache.struts.Globals.LOCALE_KEY constant.

Yes

No

name

Specifies the name of the object whose value will be used as the key for the message. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return the object whose value will be used as the key for the message.

Yes

No

property

Specifies the field, of the object specified by the name attribute, whose getter method will be called to return the object whose value will be used as the key for the message.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

Example Usage

As mentioned, there are three ways you can use the message tag. The first way, shown here, uses the key attribute to specify the key for the message that will be rendered:

<bean:message key="label.search.name"/>

The following is the second way to use the message tag:

<bean:message name="keyObj"/>

This example uses the name attribute to specify the name of an object whose value will be used as the key for the message. Remember that you can explicitly specify the scope of the object with the scope attribute.

The third way to use the message tag is shown here:

<bean:message name="keyObj" property="keyField"/>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used as the key for the message. Again, you can explicitly specify the scope of the object with the scope attribute.

The page Tag

The page tag is used to store one of a JSP’s implicit objects in a JSP scripting variable with page scope. By default, a JSP’s implicit objects cannot be “seen” by JSP tag library tags because they are not stored in a scope. Remember that JSPs get converted into a servlet and then compiled into a class file before they are run. JSP implicit objects are simply local variables in a servlet’s service( ) method. For this reason, JSP tag library tags cannot “see” the objects. However, JSP tag library tags can access objects that have been stored in a scope, thus the reason for the page tag.

The page tag works with the following implicit JSP objects:

  • application

  • config

  • request

  • response

  • session

One of the aforementioned objects must be specified with the property attribute or this tag will throw a JspException at run time.

Attributes

Attribute

Description

Accepts Scriptlet

Required

id

Specifies the name of the JSP variable that will hold the specified JSP implicit object.

No

Yes

property

Specifies the name of the JSP implicit object to store in the JSP variable. Must be application, config, request, response, or session.

Yes

Yes

Example Usage

The following snippet shows a basic usage of the page tag:

<bean:page  property="config"/>

This example simply stores a reference to the implicit config object in a page scope variable named “cfg”.

After you have used the page tag to store a JSP implicit object, you can use it with a JSP tag library tag as shown here:

<bean:write name="cfg" property="servletName"/>

Remember that the write tag wouldn’t be able to see the implicit config object without the use of the page tag.

The parameter Tag

The parameter tag is used to store a parameter from the incoming request as a String object in a JSP scripting variable with page scope. Additionally, the parameter tag can be used to store multiple parameters of the same name if the multiple attribute is specified. For example, if the incoming request has two parameters that are both named “color”, a String[ ] array would be used to capture each parameter value and then the array would be stored in the specified JSP variable.

If the incoming request does not include a parameter with the specified name attribute and the value attribute is not specified, a JspException will be thrown by this tag at run time.

Attributes

Attribute

Description

Accepts Scriptlet

Required

id

Specifies the name of the JSP variable that will hold the specified parameter(s).

No

Yes

multiple

Accepts an arbitrary value to denote that multiple parameters should be stored. If specified, parameters will be stored in a String[ ] array. If not specified and multiple parameters have the same name, the first parameter with the name specified will be stored.

Yes

No

name

Specifies the name of the parameter(s) to store in the JSP variable.

Yes

Yes

value

Specifies the default value to assign to the String object being placed in the JSP variable if the specified parameter is not present in the incoming request.

Yes

No

Example Usage

The following snippet shows a basic usage of the parameter tag:

<bean:parameter  name="clr" value="none"/>

This example attempts to find a parameter named “clr” in the incoming request and store a reference to it in a variable named “color”. If the parameter is not in the request, a new String object will be created and have the value of the value attribute assigned to it. A reference to the String will then be stored.

After you have used the parameter tag to store a parameter, you can access it with the following snippet:

Color Request Parameter: <%=color%>

This snippet simply uses a JSP expression to render the value of the color variable. Additionally, because the parameter tag places the color variable in page scope, it can be accessed by JSP tag library tags, as shown here:

Color Request Parameter: <bean:write name="color"/>

The following snippet shows an example of using the parameter tag for multiple parameters with the same name:

<bean:parameter  name="clr" multiple="true" value="none"/>

Remember that the multiple attribute accepts an arbitrary value; thus, you could use “true” as shown in the example, or any other value. As long as a value is present, a String[ ] array will be used to store the parameters. To access the first parameter stored when using the multiple attribute, use the following snippet:

First Color: <%=color[0]%>

The resource Tag

The resource tag is used to load the contents of a Web application resource and store them as a String object in a JSP scripting variable with page scope. Additionally, you have the option of specifying whether or not you simply want a java.io.InputStream reference for the resource instead of storing its contents in a String.

Web application resources are those files that are packaged as part of a Web application. For example, if your Web application is housed in a directory called c:\java\MiniHR and has the following files

  • c:\java\MiniHR\index.jsp

  • c:\java\MiniHR\WEB-INF\struts-config.xml

  • c:\java\MiniHR\WEB-INF\web.xml

then you can access the index.jsp file as “/index.jsp.” Similarly, you can access the struts-config.xml file as “/WEB-INF/struts-config.xml.” Note that Web application resource paths are relative to the application and must begin with a leading slash (/).

A JspException will be thrown by the resource tag at run time if there is a problem loading the resource.

Attributes

Attribute

Description

Accepts Scriptlet

Required

id

Specifies the name of the JSP variable that will hold the contents of the specified resource.

No

Yes

input

Accepts an arbitrary value to denote that a java.io.InputStream reference for the resource should be stored in the JSP variable instead of storing the contents of the resource in a String.

Yes

No

name

Specifies the path of the Web application resource that will be loaded. The path must begin with a leading slash (/).

Yes

Yes

Example Usage

The following snippet shows how you could use the resource tag to load the contents of a struts-config.xml file:

<bean:resource  name="/WEB-INF/struts-config.xml"/>

After you have used the resource tag to load the contents of the Struts configuration file, you can access them with the following snippet:

Struts Config file contents: <%=strutsConfig%>

This snippet simply uses a JSP expression to render the value of the String object stored in the strutsConfig variable. Additionally, because the resource tag places the strutsConfig variable in page scope, it can be accessed by JSP tag library tags, as shown here:

Struts Config file: <bean:write name="strutsConfig"/>

The following snippet shows how to use the resource tag to obtain a java.io. InputStream reference for a Web application resource:

<bean:resource                 input="true"                name="/WEB-INF/struts-config.xml"/>

Remember that the input attribute accepts arbitrary values; thus, you could use “true” as shown in the example, or any other value. As long as a value is present, a java.io.InputStream reference will be stored in the JSP variable instead of a String containing the contents of the resource file.

As you can imagine, using the resource tag is significantly cleaner and less involved than using a scriptlet to load the contents of a Web application resource.

The size Tag

The size tag is used to store the size (number of elements) of an array, java.util .Collection-based object, or java.util.Map-based object as a java.lang.Integer in a JSP scripting variable with page scope. This tag is especially useful when used in conjunction with tags in the Logic Tag Library. For example, you could use this tag to determine the size of a collection and then use the Logic Library’s iterate tag to iterate over each item in the collection.

There are three ways you can specify which collection’s size to store, as listed here and shown later in the “Example Usage” section:

  • You can use the collection attribute to specify a scriptlet that evaluates to a collection object.

  • You can use the name attribute to specify the name of a collection object that can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return a collection object. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Note that if none of the aforementioned ways is used to specify the collection whose size should be taken, a JspException will be thrown by this tag at run time.

Attributes

Attribute

Description

Accepts Scriptlet

Required

collection

Specifies a scriptlet that evaluates to an array, java.util.Collection-based object, or java.util.Map-based object.

Yes

No

id

Specifies the name of the JSP variable that will hold the size value (java.lang.Integer).

No

Yes

name

Specifies the name of the object that contains the collection whose size will be taken. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return the collection whose size will be taken.

Yes

No

property

Specifies the field, of the object specified by the name attribute, whose getter method will be called to return the collection whose size will be taken.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

Example Usage

As mentioned, there are three basic ways you can use the size tag. The first way, shown here, uses the collection attribute to specify which collection’s size to store:

<% // Example array. String[] genders = {"male", "female"}; %> <bean:size  collection="<%=genders%>"/> 

Remember that the collection attribute only accepts a scriptlet as a value. Because the collection attribute was used to specify the collection whose size will be taken, variables that are not in any particular scope can be accessed by the tag by way of a scriptlet. The following two ways of using the size tag require the collection whose size is being taken to be in a scope.

The second way to use the size tag is shown here:

<% // Example Collection. ArrayList genders = new ArrayList(); genders.add("male"); genders.add("female"); pageContext.setAttribute("genders", genders); %> <bean:size  name="genders"/>

In this example, the name attribute is used to specify the name of a JSP scripting variable that has a collection in it. Remember that all scopes will be searched in succession (page then request then session then application) to find the specified object. Optionally, you can use the scope attribute to explicitly tell the tag which scope to look in for the collection.

The third and final way to use the size tag is shown here:

<bean:size  name="employee" property="genders"/>

This example assumes you have an object named “employee” with a field called “genders”. The object’s getGenders( ) method will be called to return a collection whose size will be taken.

After you have used the size tag to store a collection’s size, you can access it with the following snippet:

Gender Count: <%=genderCount%>

This snippet simply uses a JSP expression to render the value of the Integer object stored in the genderCount variable. Additionally, because the size tag places the genderCount variable in page scope, it can be accessed by JSP tag library tags, as shown here:

Gender Count: <bean:write name="genderCount"/>

The struts Tag

The struts tag is used to store a reference to a Struts configuration object in a JSP scripting variable with page scope. When Strut’s ActionServlet initializes, it loads in struts-config.xml files and creates an internal representation of the configuration settings with configuration objects. The struts tag provides convenient access to the three most prominent of these configuration objects: FormBeanConfig, ForwardConfig, and ActionConfig.

Note that one of the formBean, forward, or mapping attributes must be specified when using this tag or a JspException will be thrown at run time. Additionally, a JspException will be thrown at run time if the specified configuration object cannot be found.

Attributes

Attribute

Description

Accepts Scriptlet

Required

formBean

Specifies the name of a FormBean whose org.apache.struts.config.FormBeanConfig object reference will be stored in the specified JSP scripting variable.

Yes

No

forward

Specifies the name of a forward whose org.apache.struts.config.ForwardConfig object reference will be stored in the specified JSP scripting variable.

Yes

No

id

Specifies the name of the JSP variable that will hold the reference to the specified Struts configuration object.

No

Yes

mapping

Specifies the name of an Action whose org.apache.struts.config.ActionConfig object reference will be stored in the specified JSP scripting variable.

Yes

No

Example Usage

Before you examine the struts tag usage, review the following sample Struts configuration file. Each of the following examples is based on this configuration file.

<?xml version="1.0"?> <!DOCTYPE struts-config PUBLIC   "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"   "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config>   <!-- Form Beans Configuration -->   <form-beans>     <form-bean name="searchForm"                type="com.jamesholmes.minihr.SearchForm"/>   </form-beans>   <!-- Global Forwards Configuration -->   <global-forwards>     <forward name="search" path="/search.jsp"/>   </global-forwards>   <!-- Action Mappings Configuration -->   <action-mappings>     <action path="/search"             type="com.jamesholmes.minihr.SearchAction"             name="searchForm"             scope="request"             validate="true"             input="/search.jsp">     </action>   </action-mappings>   <!-- Message Resources Configuration -->   <message-resources     parameter="com.jamesholmes.minihr.ApplicationResources"/> </struts-config>

There are three ways to use the struts tag. The first way, shown here, is used to store a reference to a FormBean configuration object:

<bean:struts  formBean="searchForm"/>

Notice that the value used for the formBean attribute matches the value given to the name attribute of the <form-bean> tag in the Struts configuration file shown earlier.

The second way to use the struts tag, shown next, stores a reference to a forward configuration object:

<bean:struts  forward="search"/>

Again, notice in this example that the value used for the forward attribute matches the value given to the name attribute of the <forward> tag in the Struts configuration file shown earlier.

The third way to use the struts tag stores a reference to an Action configuration object, as shown here:

<bean:struts  mapping="/search"/>

Similar to the previous two examples, the value used for the mapping attribute matches the value given to the path attribute of the <action> tag in the Struts configuration file shown earlier.

Because the struts tag stores configuration object references in page scope, the objects can be accessed both with scriptlets and JSP tag library tags.

The write Tag

The write tag is used to render the value of an object to a JSP’s output stream. Additionally, this tag can optionally format the value of the object before it is rendered by use of the format attribute.

There are two ways you can use the write tag, as listed here and shown later in the “Example Usage” section:

  • You can use the name attribute to specify the name of any object to render. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object to be rendered. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts Scriptlet

Required

bundle

Specifies the name of the application scoped org.apache.struts.util.MessageResources object that contains the format key/value pairs for the formatKey attribute.Defaults to the value stored in the org.apache.struts.Globals.MESSAGES_KEY constant.

Yes

No

filter

Accepts “true” or “false” to denote whether or not HTML-sensitive characters should be converted to their entity equivalents. For example, if this attribute is enabled, the less-than sign (<) would get converted to &lt; before it is rendered. Defaults to “true.”

Yes

No

format

Specifies the format string to use to convert the value being rendered. If not specified, write will try to use the formatKey attribute to look up the format string.

Yes

No

formatKey

Specifies the key for a format string to look up in Struts’ ApplicationResources.properties file.

Yes

No

ignore

Accepts “true” or “false” to denote whether or not to skip throwing a JspException, similar to the other tags in this library, when the object specified by the name attribute is not found. Defaults to “false.”

Yes

No

locale

Specifies the name of the session scoped java.util.Locale object to use when formatting values with the format attribute. Defaults to the value stored in the org.apache.struts .Globals.LOCALE_KEY constant.

Yes

No

name

Specifies the name of the object that will be rendered. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return the object that will be rendered.

Yes

Yes

property

Specifies the field, of the object specified by the name attribute, whose getter method will be called to return the object that will be rendered.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

Example Usage

As mentioned, there are two ways you can use the write tag. The first way, shown here, uses the name attribute to specify the name of an object to be rendered. Remember that you can explicitly specify the scope of the object with the scope attribute.

<bean:write name="bizObj"/>

The following is the second way to use the write tag:

<bean:write name="bizObj" property="bizField"/>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object to be rendered. Again, you can explicitly specify the scope of the object with the scope attribute.



 < Day Day Up > 



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2003
Pages: 134
Authors: James Holmes

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net