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="Accueil">
<result name="success">/example/Accueil.jsp</result>
</action>
<action name="FormDivers" class="example.FormDivers">
<result name="input">/example/FormDivers.jsp</result>
<result name="cancel" type="redirect">/example/Accueil.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:
Accueil.titre=Accueil
Accueil.message=Struts 2 - Conversions et validations
Accueil.FormDivers=Saisies diverses (email, url, chaine de caract\u00e8res avec contr\u00f4le du nombre de caract\u00e8res)
Form.titre=Conversions et validations
FormDivers.message=Struts 2 - Conversions et validations de types divers
Form.submitText=Valider
Form.cancelText=Annuler
Form.clearModel=Raz mod\u00e8le
Confirmation.titre=Confirmation
Confirmation.message=Confirmation des valeurs saisies
Confirmation.champ=champ
Confirmation.valeur=valeur
Confirmation.lien=Formulaire de test
xwork.default.invalid.fieldvalue=Valeur invalide pour le champ "{0}".
The [FormDiversModel.properties] file is as follows:
email.prompt=1-Tapez une adresse \u00E9lectronique (x@y.z)
email.error=Format invalide
url.prompt=2-Tapez une url (http://www.ibm.com)
url.error=Format invalide
chaine.prompt=3-Tapez une chaine de 5 caract\u00E8res
chaine.error=Format invalide
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.titre"/></title>
<s:head/>
</head>
<body background="<s:url value="/ressources/standard.jpg"/>">
<h2><s:text name="FormDivers.message"/></h2>
<s:form name="formulaire" 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.titre"/></title>
<s:head/>
</head>
<body background="<s:url value="/ressources/standard.jpg"/>">
<h2><s:text name="Confirmation.message"/></h2>
<table border="1">
<tr>
<th><s:text name="Confirmation.champ"/></th>
<th><s:text name="Confirmation.valeur"/></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="chaine"/></td>
</tr>
</table>
<br/>
<s:url id="url" action="FormDivers!input"/>
<s:a href="%{url}"><s:text name="Confirmation.lien"/></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 chaine;
// raz model
public void clearModel() {
email = null;
url1 = null;
chaine = 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="chaine" >
<field-validator type="requiredstring" short-circuit="true">
<message key="chaine.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() {
// close to the model
((FormDiversModel) getModel()).clearModel();
// result
return INPUT;
}
public String cancel() {
// cleaning 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.

