15. Example 12 – Various Conversions and Validations
The new application features the entry of various elements with Struts validators:
![]() |
- in [1], the input form
- in [2], the input confirmation
The application works similarly to the previous ones, so we will only comment on the points that differ.
15.1. The NetBeans project
The NetBeans project is as follows:
![]() |
- in [1], the application views
- [Accueil.jsp]: the home page
- [FormDivers.jsp]: the input form
- [ConfirmationFormDivers.jsp]: the confirmation page
- in [2], the message file [messages.properties] and the main Struts configuration file
- in [3]:
- [FormDivers.java]: the action that displays and processes the form
- [FormDivers-validation.xml]: the validation rules for the [FormDivers] action. This file delegates these validations to the model.
- [FormDiversModel]: the model for the [FormDivers] action
- [FormDiversModel-validation.xml]: the model's validation rules
- [FormDiversModel.properties]: the model's message file
- [example.xml]: secondary Struts configuration file
15.2. Project configuration
The project is primarily configured by the following [example.xml] file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/example" extends="struts-default">
<action name="Home">
<result name="success">/example/Home.jsp</result>
</action>
<action name="FormDivers" class="example.FormDivers">
<result name="input">/example/FormDivers.jsp</result>
<result name="cancel" type="redirect">/example/Home.jsp</result>
<result name="success">/example/ConfirmationFormDivers.jsp</result>
</action>
</package>
</struts>
It is similar to the one studied in previous versions.
15.3. Message files
The [messages.properties] file is as follows:
Home.title=Home
Home.message=Struts 2 - Conversions and validations
Home.FormMisc=Miscellaneous inputs (email, URL, string with character count validation)
Form.title=Conversions and validations
FormMisc.message=Struts 2 - Miscellaneous conversions and validations
Form.submitText=Validate
Form.cancelText=Cancel
Form.clearModel=Clear model
Confirmation.title=Confirmation
Confirmation.message=Confirmation of entered values
Confirmation.field=field
Confirmation.value=value
Confirmation.link=Test form
xwork.default.invalid.fieldvalue=Invalid value for the "{0}" field.
The [FormDiversModel.properties] file is as follows:
email.prompt=1-Enter an email address (x@y.z)
email.error=Invalid format
url.prompt=2-Enter a URL (http://www.ibm.com)
url.error=Invalid format
string.prompt=3-Enter a 5-character string
string.error=Invalid format
15.4. The input form
The [FormDivers.jsp] view is as follows:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="Form.title"/></title>
<s:head/>
</head>
<body background="<s:url value="/resources/standard.jpg"/>">
<h2><s:text name="FormDivers.message"/></h2>
<s:form name="form" action="FormDivers">
<s:textfield name="email" key="email.prompt" size="30"/>
<s:textfield name="url1" key="url.prompt" size="30"/>
<s:textfield name="chaine" key="chaine.prompt" size="10"/>
<s:submit key="Form.submitText" method="execute"/>
</s:form>
<br/>
<s:url id="url" action="FormDivers" method="cancel"/>
<s:a href="%{url}"><s:text name="Form.cancelText"/></s:a>
<br/>
<s:url id="url" action="FormDivers" method="clearModel"/>
<s:a href="%{url}"><s:text name="Form.clearModel"/></s:a>
</body>
</html>
Lines 12 through 14 are the three input fields.
15.5. The confirmation view
The confirmation view [ConfirmationFormDivers.jsp] is as follows:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="Confirmation.title"/></title>
<s:head/>
</head>
<body background="<s:url value="/resources/standard.jpg"/>">
<h2><s:text name="Confirmation.message"/></h2>
<table border="1">
<tr>
<th><s:text name="Confirmation.field"/></th>
<th><s:text name="Confirmation.value"/></th>
</tr>
<tr>
<td><s:text name="email.prompt"/></td>
<td><s:text name="email"/></td>
</tr>
<tr>
<td><s:text name="url.prompt"/></td>
<td><s:text name="url1"/></td>
</tr>
<tr>
<td><s:text name="chaine.prompt"/></td>
<td><s:text name="channel"/></td>
</tr>
</table>
<br/>
<s:url id="url" action="FormDivers!input"/>
<s:a href="%{url}"><s:text name="Confirmation.link"/></s:a>
</body>
</html>
15.6. The [FormDiversModel] template
The input fields from the [FormDivers.jsp] form are injected into the following [FormDiversModel] template:
package example;
public class FormDiversModel {
// constructor without parameters
public FormDiversModel() {
}
// fields
private String email;
private String url1;
private String string;
// clear model
public void clearModel() {
email = null;
url1 = null;
string = null;
}
// getters and setters
...
}
15.7. Model validation
Model validation is controlled by two files: [FormDivers-validation.xml] and [FormDiversModel-validation.xml].
The [FormDivers-validation.xml] file delegates validations to the following [FormDiversModel-validation.xml] file:
<!--
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//
EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-->
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//
EN" "http://localhost:8084/exemple-10/example/xwork-validator-1.0.2.dtd">
<validators>
<field name="model" >
<field-validator type="visitor">
<param name="appendPrefix">false</param>
<message/>
</field-validator>
</field>
</validators>
We have already encountered this file.
The [FormDateModel-validation.xml] file contains the following validation rules:
<!--
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//
EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-->
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//
EN" "http://localhost:8084/exemple-12/example/xwork-validator-1.0.2.dtd">
<validators>
<field name="email" >
<field-validator type="requiredstring" short-circuit="true">
<message key="email.error"/>
</field-validator>
<field-validator type="email" short-circuit="true">
<message key="email.error"/>
</field-validator>
</field>
<field name="url1" >
<field-validator type="requiredstring" short-circuit="true">
<message key="url.error"/>
</field-validator>
<field-validator type="url" short-circuit="true">
<message key="url.error"/>
</field-validator>
</field>
<field name="string">
<field-validator type="requiredstring" short-circuit="true">
<message key="string.error"/>
</field-validator>
<field-validator type="stringlength" short-circuit="true">
<param name="minLength">5</param>
<param name="maxLength">5</param>
<message key="chaine.error"/>
</field-validator>
</field>
</validators>
- lines 11–18: check the validity of the email field
- lines 15-17: verify that the email string is a valid email address
- lines 20-27: verify the validity of the url1 field
- lines 24–26: verify that the url1 string is a valid URL
- lines 29-38: check the validity of the chaine field
- lines 33-37: verify that the string chaine is exactly 5 characters long.
15.8. The [FormDivers] action
The [FormDivers] action is as follows:
package example;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
public class FormDivers extends ActionSupport implements ModelDriven, SessionAware {
// Constructor without parameters
public FormDivers() {
}
// action model
public Object getModel() {
if (session.get("model") == null) {
session.put("model", new FormDiversModel());
}
return session.get("model");
}
@SkipValidation
public String clearModel() {
// Clear the model
((FormDiversModel) getModel()).clearModel();
// result
return INPUT;
}
public String cancel() {
// clear the model
((FormDiversModel) getModel()).clearModel();
// result
return "cancel";
}
// SessionAware
Map<String, Object> session;
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
The [FormDivers] action is built on the same model as the actions discussed previously. Here, there is simply no validate method to complement the validation performed by the [FormDiversModel-validation.xml] file.

