You are on page 1of 11

Advanced Topic In Struts

Nihilent Consulting 2005

Dispatch Action
Struts Dispatch Action

Dispatch Action (org.apache.struts.actions.DispatchAction) is one


of the Built-in Actions provided along with the struts framework
No execute method
Define a method named for each possible method of the
special parameter
Same signature as the execute method
The system will call that method automatically

Nihilent Consulting 2005

Struts-Config.xml

Map incoming .do addresses to Action classes

Map return conditions to JSP pages

Declare any form beans that are being used

List special param via parameter entry in action

Restart server after modifying struts-config.xml

Nihilent Consulting 2005

Example
<action
path="/DispatchAction"
type=com.nihilent.Dispatch_Action"
parameter="parameter
input="/pages/DispatchAction.jsp
name="DispatchActionForm
scope="request validate="false">
</action>

Nihilent Consulting 2005

Struts Flow of Control

The user requests a form


The form is submitted to a URL of the form DispatchAction.do.
That address is mapped by struts-config.xml to an Action class
The designated method of the Action object is invoked
Struts forwards request to the appropriate JSP page

Nihilent Consulting 2005

Validations

Client-side validation

JavaScript code verifies format of fields

Dialog box warns users of illegal values

Submission blocked if invalid

Cannot do validation that requires much application logic


Server Side Validation

Java code on server verifies format of fields

Form is redisplayed (with warnings) if illegal values

You must do this regardless of whether or not you do

client-side validation!

Nihilent Consulting 2005

Automatic validation

Declare application-wide properties file


<message-resources parameter="resources.application/>
Add messages to properties file
inputForm.firstName=First name
Turn on the automatic validator
Use plug-in in struts-config.xml. Eg:
<plug-in
className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>

Nihilent Consulting 2005

Put validation rules in validation.xml


<formset>
<form name="orderFormBean">
<field property="firstName"
depends="required">
<arg0 key="inputForm.firstName"/>
</field>
</form>

</formset>

Have your form bean extend ValidatorForm,not ActionForm


directly
import org.apache.struts.validator.*;

public class LoginForm


extends ValidatorForm { }
Nihilent Consulting 2005

put <html:errors/> in input page


Enable JavaScript validation
Add <html:javascript/> anywhere in form
Add onsubmit="return validateBeanName(this);"
to html:form

More predefined validators

required
mask
range
maxLength, minLength
integer, float, double, long, short, byte
date
creditCard
Nihilent Consulting 2005

Internationalization(i18n) features

Two Components

Controller : Message class that references a resource bundle

containing localedependent strings.

View: JSP custom tag,<bean:message/>,which is used in the

view layer to present the actual strings managed by the


Controller

email
Nihilent Consulting 2005

Steps involved in Internationalizing

Defining the Resource Bundles

The naming format for this file is


ResourceBundleName.properties.
And a sample entry Eg. app.symbol=Symbol

Deploying the Resource Bundles


Copy all of your resource bundles into the application classpath,
which in this case is
<CATALINAHOME>/webapps/webapplicationname/
WEBINF/classes

Add a <messageresources> subelement to the


strutsconfig.xml file.

<messageresources
parameter="wiley.ApplicationResources"/>

Nihilent Consulting 2005

You might also like