Tutorials : Introducing: the Struts bean and logic Tag Libraries :

The Struts bean tags

Generally a Java scriplet in a jsp-page contains two things: data (declarations and initializations) and algorithms. The Struts bean library has been built first of all to make data available to further tag processing. Most of the tags are used to define and create beans to hold the data, but there are also a couple of tags to insert data into the HTML. The Struts bean library can be seen as an enhancement to the <jsp:useBean> capabilities. Some of the attributes, for example id, name, property, and scope, are the same as used in the <jsp:useBean> tag.

For an example let's assume that an instance of the DVD class has been stored in session scope. Using <jsp:useBean> you can get the title of the DVD like this:

<jsp:useBean id="dvd" class="hansen.playground.DVD" 
  scope="request"/>
. . .
<jsp:getProperty name="dvd" property="title"/>    

Using Struts you simply use the write tag:

<bean:write name="dvd" property="title" scope="request"/>

If you look for a counterpart to the <jsp:setProperty> tag you'll be disappointed. There is none. The basic philosophy behind the Struts bean tags are that you should only fetch data, not alter data. If you need to alter data then consider doing this in your Java code before forwarding to the jsp-page, and let a getter method access the result. If this is not possible you'll have to write Java scriplet code in the jsp-page, but remember: this is definitely against Struts principles.

The id attribute comes into play when Struts creates a new bean (just like the jsp:useBean will do if no instance is in the specified scope). As an example let's see how to create a bean containing the session object:

<bean:page id="ses" property="session"/>

To reference the bean use the name attribute with the value of the id attribute:

<bean:write name="ses" property="creationTime"/>
So the rules for attribute usage are these:
  1. id is used to define a bean
  2. name is used to refer to an existing bean (the value is either the value of an id attribute in a previous tag, or is found in application, session, request, or page scope)
  3. property is used to select a property from a bean
  4. scope tells which scope to search for the bean. If scope is not specified then the bean is searched for in page, request, session and application order)
The value for property must correspond to a getter-method in the bean. If you're in doubt about which getter-methods a bean contains you can, as an exercise, use Java's reflection technique to find the getters. Here's an example of a jsp-page that finds the getters of the session object:

<body>
<bean:page id="bean" property="session"/>

<%
Object s = pageContext.getAttribute("bean");
Class c = s.getClass();
Method[] m = c.getMethods();
for (int i = 0; i < m.length; i++) {
  String name = m[i].getName();
  if (name.length() > 3 && name.substring(0,3).equals("get") )
    out.println("<br>Method: " + m[i].getName());
}  
%>

</body>

- Figure 6: Introspection of the session bean

The output you'll get from this page is:

Method: getClass 
Method: getValue 
Method: getAttribute 
Method: getId 
Method: getAttributeNames 
Method: getServletContext 
Method: getMaxInactiveInterval 
Method: getLastAccessedTime 
Method: getCreationTime 
Method: getSessionContext 
Method: getValueNames 

- Figure 7: Output from introspection

With the bean tags you can create a lot of different beans (which then can be accessed with the logic tags--see the next section). Here are some useful examples of the bean tags:

A bean with a String constant
<bean:define id="string" 
  value="Struts in Javaboutique"/>
Copy an existing bean
bean:define id="copy" name="dvd"/>
Copy an existing bean and create it in a new scope
<bean:define id="copy" name="dvd" 
  scope="request" toScope="session"/>
Copy a single property from a bean
<bean:define id="title" 
  name="copy" property="title"/>
Copy a cookie
<bean:cookie id="who" name="cookiename"/>
Copy a request header
<bean:header id="ref" name="referer"/>
Copy data from the struts-config file
<bean:struts id="form" formBean="bookForm" />
Copy a request parameter
<bean:parameter id="req" name="item" />

- Figure 8: Creating beans

As you can see, the authors of the Struts taglibs have tried to make the most often used data accessible through tags.

The ActionForm beans are also available to Struts through the names given in the struts-config file. An ActionForm defined like this in the config file:

<form-bean name="bookForm" type="BookForm"/>

can be referenced with name="bookForm"--e.g.

<bean:define id="title" name="bookForm" property="title"/> 

Two of the tags in the bean library are used to output data to the response page:

bean:write
which converts a bean or a property in a bean to a string 
and sends it to the response page

<bean:write name="string"/>
<bean:write name="dvd" property="title"/>

bean:message
which looks up a key in the 
ApplicationResources.properties file

<bean:message key="menu.title" />

- Figure 9: Outputting data

To see a listing of all the bean tags with their attributes, see Struts' own documentation.

To summarize: most of the bean library tags locate data and make them ready for other tags. The write tag can be used to output the data. But in order to create dynamic content in our response page we need tags that can express conditions and loops. Enter the logic library.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.