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 :

Introducing: the Struts bean and logic Tag Libraries

by Keld H. Hansen

Introduction

In my previous articles about Jakarta Struts--"Stepping through Jakarta Struts" and "Coding your second Jakarta Struts Application"--the focus was mostly on the MVC application architecture, its basic parts (e.g. the ActionForm and Action classes) and how this was glued together using the struts-config file. When we built the jsp-pages we also used several jsp tags from the Struts' html tag library, but Struts' other tag libraries were only shown and discussed in a few lines. In this article I'll introduce two of the other tag libraries: the bean and logic libraries. The information relates to version 1.0.2. If you're interested in the last Struts library--template--then I'd suggest you look at the documentation found on Struts' home page.

Let's warm up by refreshing the syntax of JSP custom tags. There are two formats:

The body-less format:

<tagPrefix:tagName att1="value1" att2="value2" … />    

The format with a body:

<tagPrefix:tagName att1="value1" att2="value2" … >
. . . body of the tag . . .
</tagPrefix:tagName> 

The tagPrefix specifies which tag library the tag belongs to. Two tags from separate libraries may therefore have the same tagName. A tag library is defined like this in your jsp page (the example defines the Struts html tag library):

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 

The syntax is not the most user-friendly, so it's something you'd like to cut and paste, but it contains the tag prefix and the location of file that contains the tag library description.

Here are a few tag examples--one from each of the html, bean, and logic libraries--the last with a body:

<html:text property="town" size="30"/>

<bean:write name="car"/>

<logic:present name="lastName" scope="request">
Welcome again! 
</logic:present>

You should view a tag like a small Java component. The bean:write tag above can be read like this: "invoke the write-method of the bean library using the value car for the name-parameter". If a tag has a body then the body is part of the data given to the tag.

Why would you want to use tags in your jsp-pages?

First of all you'd use it to get rid of Java code in your jsp-pages. Java code and HTML don't mix well. If you have graphical designers to build the HTML, then they're definitely not interested in Java code. Even when it's the Java programmer that constructs the jsp-pages, it's an advantage to get rid of Java code. Small syntax errors in the Java code can result in meaningless messages from the jsp-engine. Try, for example, these lines on your Java web server (there's a missing opening curly parentheses):

<%
int i = 5;
if (i==5) 
  int j = 4; 
}
%>

On my Tomcat server I get this message:

message Internal Server Error
description The server encountered an internal error (Internal Server Error)
  that prevented it from fulfilling this request.
exception org.apache.jasper.JasperException: Unable to compile class for JSP

An error occured between lines: 11 and 13 in the jsp file: /error1.jsp

Generated servlet error:
D:\Apache\jakarta-tomcat-4.0.1\work\localhost\strutstags\error1$jsp.java:69: 
  'try' without 'catch' or 'finally'.
                out.write("\r\n\r\n</body>");
                ^
D:\Apache\jakarta-tomcat-4.0.1\work\localhost\strutstags\error1$jsp.java:73: 
 Type expected.
        } catch (Throwable t) {
          ^
2 errors

The error messages don't give you any help. They refer to the servlet code that was generated from the jsp-page, so line numbers and statements are meaningless to the jsp-programmer. If you have an error like this in a jsp-page with several hundred lines then--and I know this from experience--it can take you a very long time to locate the error. So the less Java code you have in your jsp-pages the better.

Many programmers find the jsp custom tag-syntax awkward, and that it makes jsp-pages more difficult to read. This is very much a matter of taste, but in some situations the tags will produce smaller, more readable pages. Le me show you this in the first example.

 

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%.

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
Oracle: Eating its Own Open Source Food
Big Money and Open Source May Not Compute
Open Source Embrace Gives Sun New Fans
NetBeans, OpenSolaris Also in Spotlight at JavaOne

Taming Trees: Building Branching Structures
Clean Up Function Syntax Mess with decltype
Sutter Speaks: The Future of Concurrency
INTEL SCAVENGER HUNT, LENOVO X300 AND APPLE IPOD TOUCH GIVEAWAY (the "Giveaway")
Comparing Multi-Core Processors for Server Virtualization
Intel® Desktop Business Computing Solutions
Intel: What Downturn?
Managing the Evolving Data Center
Implement Drag and Drop in Your Windows Forms Applications
Processing Linked Web Data with XSLT

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: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
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