Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 6
Let's consider a simple Struts 2 action, called FirstAction, with three fields:
name:Stringage:Integergender:Gender<Enum>(I'll explain later why objects are preferred to simple Java types.) Depending on the value of gender:Gender<Enum>, FirstAction opens either Male.jsp or Female.jsp, as shown in Listing 1:
<struts>
<package name="test" extends="struts-default">
<action name="First" class="struts2validation.FirstAction">
<interceptor-ref name="defaultStack"/>
<result name="female">/Female.jsp</result>
<result>/Male.jsp</result>
</action>
</package>
</struts>
The name attribute can be omitted from one of the two <result> tags. That result's name automatically defaults to success.
Once an HTML form is submitted, the request is intercepted by all interceptors from the defaultStack, including validation, which is an XWork ValidationInterceptor. Starting with Struts 2.0.4, the XWork ValidationInterceptor is extended by AnnotationValidationInterceptor, which adds functionality to disable validation of action methods that are annotated by a @SkipValidation annotation.
The validation framework looks for an XML validation configuration whose name is ActionClass-validation.xml, where ActionClass is the name of the related action class. The validation configuration must be located in the same package as the action class itself. Because a single action class could be used in different actions, it is possible to have a validation configuration for each of the actions, in which case the file should be named ActionClass-ActionName-validation.xml. Struts 2 supports object-oriented programming concepts and considers validation configurations for all classes that the action extends and all interfaces that it implements.
Now I'll demonstrate a simple validation. I'll create a validation configuration for the FirstAction class and call it FirstAction-validation.xml.
First, though, I need to create a FirstInput.jsp file with a form, using Struts 2 custom tags. Custom tags use templates for rendering HTML code. Themes are collections of templates grouped by their output methods. I'll discuss themes later. Right now, you only need to know that four themes come with Struts 2:
simple (so simple that it doesn't provide validation support)
xhtmlcss_htmlajaxThe example form will use the xhtml theme. The <s:actionerror> tag displays any action errors. The <form> tag produces an HTML form with two HTML inputs (a <s:textfield> tag and a <s:submit> tag for a Submit button). Listing 2 shows the form for FirstInput.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body><s:actionerror/>
<s:form action="First" theme="xhtml">
<s:textfield label="Name" name="name" required="true"/>
<s:textfield label="Age" name="age" required="true"/>
<s:textfield label="Gender (Male/Female)" name="gender"/>
<s:submit/>
</s:form>
</body>
</html>
Note that the required attribute in the <s:textfield> element has nothing to do with validation. It is used by some templates to display an asterisk next to the field.
Gender is a Java Enum, which needs to be handled specifically. Type conversion in Struts 2 is beyond this article's scope, so I'll just create
a FirstAction-conversion.properties file for FirstAction in the same folder, with this content:
gender=com.opensymphony.xwork2.util.EnumTypeConverter
FirstAction, shown in Listing 3, uses simple logic to distinguish between male and female:
public class FirstAction extends ActionSupport {
public String execute() throws Exception {
if(Gender.Female.equals(gender)) {
return "female";
} else {
return SUCCESS;
}
} ...
If the user enters Male in the form, the success result (which is mapped to Male.jsp) returns. Even without seeing any validation work yet, you can probably already get the
feel of Struts 2 validation. If you enter a nonnumeric value in the age field and submit the form, a validation message will
appear: "Invalid field value." The same is true for the gender field: you can only enter Male or Female there; otherwise a validation message appears. This is the result of the conversionError interceptor from the defaultStack making sure that all parameters are of the type that the action can accept.
More