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.

XML error: undefined entity at line 19
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%.

Linux Vendors Head to the Cloud in Search of Cash
iPhone 3GS: Overheating Fears, OS Update Nears
PostgreSQL 8.4 Revs Up Database Admin, Security
PHP 5.3 Accelerates PHP
Sun Releases NetBeans 6.7 IDE for Java, PHP
Why Firefox Doesn't Take Google Chrome Features
First Major PHP Update in Years Coming Soon
Red Hat CEO Calls on Oracle to Keep Java Open
Google Widens AdSense for iPhone, Android Apps
Eclipse Galileo Releases 33 Open Source Projects

A Taste of JavaFX for the Uninitiated
A Guide to Caching and Compression for High Performance Web Applications
How User-Centered Design Can Put User Stories in Proper Context
Explore C# 4's New Dynamic Types and Named/Optional Parameters
Enterprise Architecture: The Journey Begins Here, Part 2
Create a Syslog Sender/Receiver Using the MS Winsock Control
AMD CodeAnalyst Helps Developers Optimize and Tune Applications
Securing Microsoft's Cloud Infrastructure
Introducing the Azure Services Platform
An Introduction to Microsoft .NET Services for Developers

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs