Struts 2.x : [A Framework from ASF] :: Validation Using Java
Struts 2.x Validation Using Java |
Example for Struts 2.x Validation Using Java
Example for Validation using Java in Struts |
Project Structure
Employee.java
package com.javaskool.struts;
import com.opensymphony.xwork2.ActionSupport;
public class Employee extends ActionSupport
{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String execute()
{
if(name.equals("tom"))
return SUCCESS;
else
return ERROR;
}
public void validate()
{
if(name==null || name.trim().equals(""))
{
addFieldError("name","Name is Required");
}
if(age<20 || age >60)
{
addFieldError("age","Age should be between 20 and 60");
}
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Employee Form</title>
</head>
<body>
<s:form action="emp1" method="post">
<s:textfield name="name" label="Name : " size="20" />
<s:textfield name="age" label="Age : " size="20"/>
<s:submit name="submit" label="Submit" align="center"/>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Success</title>
</head>
<body>
Employee Info has been submitted successfully...
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Error</title>
</head>
<body>
Check your name, You have enter wrong name.
</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"></constant>
<package name="p1" extends="struts-default">
<action name="emp1" class="com.javaskool.struts.Employee" method="execute">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/error.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>Struts2WithValidation</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
If you enter any other name than “tom”, will return below screen.
Click here to download complete code with jar files [3.6 MB]
Recent Comments