Professional Documents
Culture Documents
freedom of innovation
Jakarta Struts
An Apache Software Foundation Project
• ActionForm Beans
– Extends the ActionForm class
– Create one for each input form in the application
– If defined in the ActionMapping configuration file,
the Controller Servlet will perform the following:
• Check session for instance of bean of appropriate class
• If no session bean exists, one is created automatically
• For every request parameter whose name corresponds
to the name of a property in the bean, the corresponding
setter method will be called
• The updated ActionForm bean will be passed to the
Action Class perform() method when it is called, making
these values immediately available
Model Components
• Continued…
– Define a property (with associated getXxx() and setXxx()
methods) for each field that is present in the form. The field
name and property name must match according to the usual
JavaBeans conventions
– Place a bean instance on your form, and use nested
property references. For example, you have a "customer"
bean on your Action Form, and then refer to the property
"customer.name" in your JSP view. This would correspond
to the methods customer.getName() and
customer.setName(string Name) on your customer bean
Model Components
• ApplicationResources.properties
– Contains the messages in the default
language for your server. If your default
language is English, you might have an
entry like this: prompt.hello=Hello
• ApplicationResources_xx.properties
– Contains the same messages in the
language whose ISO language code is "xx
• Forms and FormBean interactions
– HTML Forms and their limitations
– Errors not easily handled
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html>
<head> <title> <bean:message key="logon.title"/> </title>
<body bgcolor="white">
<html:errors/>
<html:form action=“/logonpath.do">
<table border="0" width="100%">
<tr> <th align="right"> <html:message key="prompt.username"/>
</th>
<td align="left"> <html:text property="username" size="16"/>
</td> </tr>
<tr> <td align="right"> <html:submit> <bean:message
key="button.submit"/> </html:submit> </td>
View Components
<HTML>
<BODY>
<FORM>
<TABLE WIDTH="100%">
<TR><TD>First Name</TD>
<TD><INPUT TYPE="TEXT" NAME="Name" SIZE="40" MAXLENGTH="40"></TD></TR>
<TR><TD>Street Address</TD>
<TD><INPUT TYPE="TEXT" NAME="Address" SIZE="40" MAXLENGTH="40"></TD></TR>
<TR><TD>City</TD>
<TD><INPUT TYPE="TEXT" NAME="City" SIZE="20" MAXLENGTH="20"></TD></TR>
<TR><TD>State</TD>
<TD><INPUT TYPE="TEXT" NAME="State" SIZE="2" MAXLENGTH="2"></TD></TR>
<TR><TD>Postal Code</TD>
<TD><INPUT TYPE="TEXT" NAME="ZipCode" SIZE="9" MAXLENGTH="9"></TD></TR>
<TR><TD ALIGN=CENTER><INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Save"></TD>
<TD><INPUT TYPE="RESET" NAME="Reset" VALUE="Cancel"></TD></TR>
</TABLE>
</FORM>
</BODY>
</HTML>
HTML Tags – Typical Struts Form
<HTML:HTML>
<BODY>
<HTML:FORM Action="/CustomerForm" focus=“name” >
<TABLE WIDTH="100%">
<TR><TD><bean:message key="customer.name"/></TD>
<TD><HTML:TEXT property="name" size="40" maxlength="40" /></TD></TR>
<TR><TD><bean:message key="customer.address"/></TD>
<TD><HTML:TEXT property="address" size ="40" maxlength ="40" /></TD></TR>
<TR><TD><bean:message key="customer.city"/></TD>
<TD><HTML:TEXT property="city" size ="20" maxlength ="20" /></TD></TR>
<TR><TD><bean:message key="customer.state"/></TD>
<TD><HTML:TEXT property="state" size ="2" maxlength ="2" /></TD></TR>
<TR><TD><bean:message key="customer.zip"/></TD>
<TD><HTML:TEXT property="zip" size ="9" maxlength ="9" /></TD></TR>
<TR><TD ALIGN=CENTER><html:submit property="action" value ="Save"/></TD>
<TD><html:reset property="action" value ="Reset"/></TD></TR>
</TABLE>
</HTML:FORM>
</BODY>
</HTML:HTML>
Bean Tags
• The "struts-bean" tag library provides substantial enhancements
to the basic capability provided by <jsp:useBean>, as discussed
in the following sections:
– Bean Properties - Extended syntax to refer to JavaBean properties
with simple names (same as the standard JSP tags
<jsp:getProperty> and <jsp:setProperty>), nested names (a
property named address.city returns the value retrieved by the Java
expression getAddress().getCity()), and indexed names (a property
named address[3] retrieves the fourth address from the indexed
"address" property of a bean).
– Bean Creation - New JSP beans, in any scope, can be created
from a variety of objects and APIs associated with the current
request, or with the servlet container in which this page is running.
– Bean Output - Supports the rendering of textual output from a
bean (or bean property), which will be included in the response
being created by your JSP page.
Tag Name Description
cookie Define a scripting variable based on the value(s) of the specified request cookie.
define Define a scripting variable based on the value(s) of the specified bean property.
header Define a scripting variable based on the value(s) of the specified request header.
include Load the response from a dynamic application request and make it available as a bean.
parameter Define a scripting variable based on the value(s) of the specified request parameter.
write Render the value of the specified bean property to the current JspWriter.
Bean Tag Example
<table border="2">
<tr>
<th align="left"><bean:message key=“imagebroker.lob”/></th>
<th align="left"><bean:message key=“imagebroker.unitnbr”/></th>
<th align="left"><bean:message key=“imagebroker.onbase_dns”/></th>
</tr>
<logic:iterate id="image" property="collection"
name="ImageLocationListForm">
<tr>
<td><a href="ImageLocationListForm.do?lob=<bean:write name='image'
property='lob'/>
&unitnbr=<bean:write name='image' property='unitnbr'/>
&onbase_dns=<bean:write name='image' property='onbase_dns'/>" >
<bean:write name="image" property="lob"/></a></td>
<td><bean:write name="image" property="unitnbr"/></td>
<td><bean:write name="image" property="onbase_dns"/></td>
</tr>
</logic:iterate>
</table>
Logic Tags
• The Logic tag library contains tags that
are useful in managing conditional
generation of output text, looping over
object collections for repetitive
generation of output text, and
application flow management.
• Can do value comparison, substring
matching, etc
Logic Tags - Example
<html:html>
<head>
<title><bean:message key="imagebrokerlink.title"/></title>
<META name="GENERATOR" content="IBM WebSphere Studio">
</head>
<body>
<html:form action="/ImageLocationForm" >
<center>
<font size=3>
<br>
<b>
<logic:notEqual property="action" name="ImageLocationForm" value="Insert">
<bean:message key="imagelocationdetail.title"/>
</logic:notEqual>
<logic:equal property="action" name="ImageLocationForm" value="Insert">
<bean:message key="imagelocationinsert.title"/>
</logic:equal>
…
…
Template Tags
• The Template tag library contains three
tags: put, get, and insert. Put tags put
content into request scope, which is
retrieved by a get tag in a different JSP
page (the template). That template is
included with the insert tag.
Custom Tags
<tag>
<name>doctype</name>
<tagclass>com.pri.brokertag.ImageBrokerDoctype</tagclass>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
Custom Tags – Tag Class
<taglib>
<taglib-uri>WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>
Application Resources File
Ted Husted
Special thanks
http://www.applabs.com/
About Us