advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

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.

 DevX Skillbuilding from IBM developerWorks
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

RIM Ups Ante With Mobile Software Push
Novell Readies Silverlight Clone for Linux
Yahoo Pitches The 'Next Generation of Search'
Alfresco's Latest ECM: Prying Open a Sector?
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads for the JON 2.0
Out with the Old, in with the New at JavaOne
Trolltech Expands WebKit Footprint

Create Secure Java Applications Productively, Part 1: Use Rational Application Developer and Data Studio
.NET Building Blocks: Custom User Control Fundamentals
Secure Internet File-Sharing with PHP, MySQL, and JavaScript
Getting Started with TBB on Windows
Moving to VoIP: Should You Go It Alone?
Introduction to the WPF Command Framework
7.0, Microsoft's Lucky Version?
Will Hyper-V Make VMware This Decade's Netscape?
Eliminate Fragmentation Frustration with Netbiscuits
Taming Trees: Building Branching Structures

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES