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