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.

 BlackBerry Application Development Resources
 Microsoft Visual Studio 2010 Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow
Facebook Makes Major PHP Push With HipHop
Free Ride Over for Microsoft Azure Users
Drupal Opens the Garden to Boost CMS
Oracle Talks Plans for Linux, Solaris
Azure Makes Cloud Computing Innovation Safe
Red Hat's JBoss Looks Ahead
Microsoft Readies Two Windows Phone Systems?

Apple Surveying iPhone Developers? Happiness With The App Store
HTML 5 Leaves Client Storage Open to Web Attacks
Basic Market Forecasting with Encog Neural Networks
Location-Aware App Review
The Future of Web Content -- HTML5, Flash, and Mobile Apps
Moonlight 3.0 Preview Offered For Rich Internet Apps on Linux and Unix
Why a Moderator is Key in the Engineering Review Process, Part II
Windows 7 Features Your Clients Will Need on Day One
What Your Clients Will Ask About Windows 7
Melissa Data Helps Developers Improve the Quality of Business Data

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


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers