Professional Documents
Culture Documents
rc020 010d Struts2 - 0
rc020 010d Struts2 - 0
com
#20
CONTENTS INCLUDE:
n
Conguring the Web Application
n
Actions
n
Conguring Actions
n
Result Types
n
Interceptors
n
Hot Tips and more...
Struts2 is the next generation of model-view-controller web
application frameworks. It aims at providing increased productivity
through reduced XML conguration, smart conventions, and a
modular and loosely-coupled architecture. This refcard refers to
Struts2 version 2.0.x.
To congure Struts2, a lter needs to be congured in the
applications web.xml le:
<web-app>
<flter>
<flter-name>struts2</flter-name>
<flter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</flter-class>
</flter>
<flter-mapping>
<flter-name>struts2</flter-name>
<url-pattern>/*</url-pattern>
</flter-mapping>
</web-app>
Actions are the basic building blocks of Struts:
public class UserAction {
private int age;
private UserService service;
public int getAge() { return age; }
public void setAge( int age ) { this.age = age; }
public void setUserService( UserService service ) {
this.service = service;
}
public String execute() throws Exception {
service.updateAge(age);
return "success";
}
}
Features of a Struts2 action are:
n
An action doesnt need to extend classes or implement
interfaces (its a POJO)
n
Use getters and setter to access data in your view and
transfer data to and from the action
n
Data conversion is done for you by Struts2 (all basic type
conversion is available, and you can dene your own more
complex conversions)
The struts.xml le (accessed via the classpath) provides
conguration for Struts2.
<struts>
<constant name="struts.devMode" value="true" />
<package name="test" extends="struts-default"
namespace="/tests" >
<default-interceptor-ref name="basicStack" />
<global-results>
<result name="error" type="dispatcher">
/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping
exception="java.lang.Exception" result="error" />
</global-exception-mappings>
Actions, continued
n
Pluggable dependency injection is used for services
( injecting a Spring Framework-managed bean is as simple
as placing a setter on the action with a name that matches
the beans id in the Spring conguration)
n
The method providing the logic is called execute by
conventionbut it could be called anythingas long
as it returns a String and has no parameters (it can also
throw Exception)
ABOUT STRUTS2
CONFIGURING THE WEB APPLICATION
ACTIONS
CONFIGURING ACTIONS
S
t
r
u
t
s
2
w
w
w
.
d
z
o
n
e
.
c
o
m
G
e
t
M
o
r
e
R
e
f
c
a
r
d
z
!
V
i
s
i
t
r
e
f
c
a
r
d
z
.
c
o
m
n
Authoritative content
n
Designed for developers
n
Written by top experts
n
Latest tools & technologies
n
Hot tips & examples
n
Bonus content online
n
New issue every 1-2 weeks
Subscribe Now for FREE!
Refcardz.com
Get More Refcardz
(Theyre free!)
Struts2
By Ian Roughley
3
DZone, Inc. | www.dzone.com
Struts2
tech facts at your fingertips
Validation Annotations, continued
b. type: Validator.FIELD or Validator.SIMPLE (defaults
to ValidatorType.FIELD)
@Validations(
requiredFields = {
@RequiredFieldValidator(
feldname="userName",
message="Username is required")},
emails = {
@EmailValidator(feldName="emailAddress",
message="Email address is required")}
)
Conversion Annotations
Similar to validation annotations, when using conversion
annotations you must add the class-level @Conversion
annotation to the class.
Once this is complete, conversion annotations from Table
4 can be used. These annotations can be applied at the
method or property level.
Updated documentation on the validators can be found at:
http://struts.apache.org/2.x/docs/annotations.html.
The @Validations validator allows you to specify multiple validators
on the execute() method. It has the following parameters:
Table 3. Validation Annotations
Table 4. Conversion Annotations
Annotation Name Description
@ConversationErrorFieldValidator
(a)(b)
Validates that there are no conversion errors
for a eld.
@DateRangeFieldValidator (a)(b) Validates that a date falls between a range.
nmin (not required)the minimum valid date
nmax (not required)the maximum valid date
@DoubleRangeFieldValidator(
message = "Please enter a date this year",
key = "validate.thisYear",
min = "2008/01/01",
max = "2008/12/31")
@DoubleRangeFieldValidator
(a)(b)
Validates that a double falls between a range.
nminInclusive (not required)the inclusive
minimum valid date
nmaxInclusive (not required)the inclusive
maximum valid date
nminExclusive (not required)the exclusive
minimum valid date
nmaxExclusive (not required)the exclusive
maximum valid date
@DateRangeFieldValidator(
message = "Please enter a date this year",
key = "validate.thisYear",
minInclusive = "2008/01/01",
maxInclusive = "2008/12/31")
@EmailValidator (a)(b) Validates that the email has a valid format.
@ExpressionValidator Validates that an expression evaluates to true.
nexpressionthe OGNL expression to evaluate
@ExpressionValidator(
message = "Please confrm password",
key = "confrm.password",
shortCircuit = true,
expression =
"password.equals(confrmPassword)" )
@FieldExpressionValidator (a) Validates that a eld expression evaluates to true.
nexpressionthe OGNL expression to evaluate
IntRangeFieldValidator (a)(b) Validates that an int falls between a range.
nmin (not required)the minimum valid date
nmax (not required)the maximum valid date
@RequiredFieldValidator (a)(b) Validates that the eld is not null.
@RegexFieldValidator (a)(b) Validates that a string eld matches a regular
expression.
nexpressionthe regular expression to evaluate
@RequiredStringValidator (a)(b) Validates that the eld is not empty (i.e. not
null and length > 0)
@StringLengthFieldValidator (a)(b) Validates that a String is of a certain length.
ntrim (not required)removes white space padding
nminLength (not required)the minimum length
the String must be
nmaxLength (not required)the maximum length
the String must be
@UrlValidator (a)(b) Validates that the eld has a valid URL format
@VisitorFieldValidator (a) Steps into action properties to continue validation.
This keeps validation for models separate and
re-useable across multiple actions.
ncontext (not required)the validation context.
Multiple contexts allow for different validation
rules in different circumstances (defaults to
action context)
nappendPrex (not required)whether the property
name (of the action) should be pre-pended
to the eld name. i.e. user.name vs. name
(defaults to true).
@VisitorFieldValidator(
message = "Error validating User",
key = "user.error",
shortCircuit = true,
context = "model",
appendPrefx = true)
@CustomValidator (a)(b) Used to specify a custom validator. In addition,
an array of @ValidationParameter annotations
can be used to pass parameter to the custom
validator. Custom validators extend the Valida-
torSupport or FieldValidatorSupport class.
@CustomValidator(
type ="myUserValidator",
feldName = "user",
parameters = {
@ValidationParameter(
name = "source",
value = "admin" ) }
)
Parameter Description
requiredFields a list of RequiredFieldValidators
customValidators a list of CustomValidators
conversionErrorFields a list of ConversionErrorFieldValidators
dateRangeFields a list of DateRangeFieldValidators
emails a list of EmailValidators
eldExpressions a list of FieldExpressionValidators
intRangeFields a list of IntRangeFieldValidators
requiredStrings a list of RequiredStringValidators
stringLengthFields a list of StringLengthFieldValidators
urls a list of UrlValidators
visitorFields a list of VisitorFieldValidators
regexFields a list of RegexFieldValidator
expressions a list of ExpressionValidator
Annotation Name Description
@TypeConversion Provides custom conversion for a property or method.
Custom converters extend the StrutsTypeConverter class.
nkey (not required)the property or key name (defaults to
property name)
ntype (not required)determines the scope of the conversion:
ConversionType.APPLICATION or ConversionType.CLASS
(defaults to ConversionType.CLASS)
nrule (not required)the conversion rule:
ConversionRule.PROPERTY, ConversionRule.MAP,
ConversionRule.KEY, ConversionRule.KEY_PROPERTY,
ConversionRule.ELEMENT, ConversionRule.CREATE_IF_NULL
(defaults to ConversionRule.PROPERTY)
nconverter (converter or value required)the class name of
the converter
nvalue (converter or value required)the value to set when using
ConversionRule.KEY_PROPERTY
@TypeConversion(
type = ConversionType.APPLICATION,
property = "java.util.Date",
converter =
"com.opensymphony.xwork2.util.XWorkBasic-Converter")
There are more conversion annotations avail-
able, although with generics they are mostly
unused. If youre interested, the full list can
be found at http://struts.apache.org/2.x/
docs/annotations.html.
Hot
Tip
4
DZone, Inc. | www.dzone.com
Struts2
tech facts at your fingertips
Result Types, continued