Struts 2.x : [A Framework from ASF] :: Validation Using XML
Struts 2.x Validation Using XML |
Example for Validation using XML in Struts
Example for Validation using XML in Struts |
Project Structure
Person.java
package com.javaskool;
public class Person {
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private int age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String toString(){
return "<br/>First Name: " + getFirstName() +
", <br/>Last Name: " + getLastName() +
", <br/>Email: " + getEmail() +
", <br/>Phone Number: " + getPhoneNumber() ;
}
}
EmpRegistration.java
package com.javaskool;
import com.opensymphony.xwork2.ActionSupport;
public class EmpRegistration extends ActionSupport {
private static final long serialVersionUID = 1L;
private Person personBean;
@Override
public String execute() throws Exception {
return SUCCESS;
}
public Person getPersonBean() {
return personBean;
}
public void setPersonBean(Person personBean) {
this.personBean = personBean;
}
}
EmpRegistration-validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<validator type="requiredstring">
<param name="fieldname">personBean.firstName</param>
<message>First name is required.</message>
</validator>
<validator type="requiredstring">
<param name="fieldname">personBean.lastName</param>
<message>Last name is required.</message>
</validator>
<validator type="requiredstring">
<param name="fieldname">personBean.email</param>
<message>Email address is required.</message>
</validator>
<validator type="email">
<param name="fieldname">personBean.email</param>
<message>Email address not valid.</message>
</validator>
<validator type="requiredstring">
<param name="fieldname">personBean.phoneNumber</param>
<message>Phone number is required.</message>
</validator>
<validator type="regex">
<param name="fieldname">personBean.phoneNumber</param>
<param name="expression"><![CDATA[\d{3}-\d{3}-\d{4}]]></param>
<message>Phone number must be entered as 999-999-9999.</message>
</validator>
</validators>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome to Struts 2.x Application</title>
</head>
<body>
<h3>Welcome to Struts 2.x Application having Validation using XML </h3>
<p><a href="register.jsp">Click here to register.</p>
</body>
</html>
success.jsp
You are successfully Registered.
register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
<%--
The Struts 2 s:head tag can be used to provide CSS that includes a style for the
error message.
Add <s:head /> to register.jsp before the closing HTML </head> tag.
--%>
<s:head />
</head>
<body>
<h3>Register with Javaskool by completing below form.</h3>
<s:form action="register">
<s:textfield name="personBean.firstName" label="First name" />
<s:textfield name="personBean.lastName" label="Last name" />
<s:textfield name="personBean.email" label ="Email"/>
<s:textfield name="personBean.phoneNumber" label="Phone number" />
<s:submit/>
</s:form>
</body>
</html>
struts.xml
<?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.devMode" value="true" />
<package name="myPack1" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="register" class="com.javaskool.EmpRegistration" method="execute">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>06_Struts2WithRegistraionValidationUsingXML</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
Click here to download complete code with jar files [3.8 MB]
Recent Comments