<validators>
<field name="bar">
<field-validator type="required">
<message>You must enter a value for bar.</message>
</field-validator>
</field>
</validators>
Non-field validators only add action level messages. Non-field validators are mostly domain specific and therefore offer custom implementations. The most important standard non-field validator provided by XWork is ExpressionValidator.<validators>
<validator type="expression">
<param name="expression">foo lt bar</param>
<message>Foo must be greater than Bar.</message>
</validator>
</validators>

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employee Form - Struts2 Demo | dineshonjava.com</title>
<s:head />
</head>
<body>
<h2>Employee Form</h2>
<s:form action="employee" method="post" validate="true">
<s:textfield name="name" key="name" size="20" />
<s:textfield name="age" key="age" size="20" />
<s:textfield name="email" key="email" size="20" />
<s:textfield name="telephone" key="telephone" size="20" />
<s:submit method="addEmployee" key="label.add.employee" align="center" />
</s:form>
</body>
</html>
name= Name
age= Age
email= Email
telephone= Telephone
label.add.employee=Add Employee
errors.invalid=${getText(fieldName)} is invalid.
errors.required=${getText(fieldName)} is required.
errors.number=${getText(fieldName)} must be a number.
errors.range=${getText(fieldName)} is not in the range ${min} and ${max}.
For this tutorial, we will create an Action class called EmployeeAction which will contain few fields. Create a file EmployeeAction.java in package com.dineshonjava.struts2.actionpackage com.dineshonjava.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Dinesh Rajput
*
*/
public class EmployeeAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
private Integer age;
private String email;
private String telephone;
public String addEmployee() {
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
Note that EmployeeAction class has fields name, email, telephone and age. Also it has a method called addEmployee() which doesn’t have any logic, it just return SUCCESS.<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="myapp" />
<package name="default" extends="struts-default" namespace="/">
<action name="employee" class="com.dineshonjava.struts2.action.EmployeeAction">
<result name="success">/success.jsp</result>
<result name="input">/employee.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employee Page - Struts2 Demo | dineshonjava.com</title>
</head>
<body>
<h2>Employee Added Successfully.</h2>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Welcome Page - Struts2 Demo | dineshonjava.com</title> </head> <body> <s:a href="employee.jsp">Add Employee</s:a> </body> </html>Adding Validation Logic
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="errors.required" />
</field-validator>
</field>
<field name="age">
<field-validator type="required">
<message key="errors.required" />
</field-validator>
<field-validator type="int">
<param name="min">18</param>
<param name="max">65</param>
<message key="errors.range"/>
</field-validator>
</field>
<field name="email">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
<field-validator type="email">
<message key="errors.invalid" />
</field-validator>
</field>
<field name="telephone">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
</field>
</validators>
<s:form action="employee" method="post" validate="true">
...
</s:form>




Labels: struts2