In this Tutorial we are performing server side validation using Struts2 framework on various fields eg. name,email etc.
Project Structure in Project Explorer:
Firstly Download Struts2 Jars
How to Use Struts2 Jars by two ways:
1. Put all downloaded jars into lib folder of your project.
2. The way i am using Jars, For More Details Read How to set Struts2 Environment
Web Deployment Assembly contactus.jsp
create a user input page.
<%@ 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>Contact Us</title>
</head>
<body bgcolor="brown">
<h1>Contact Information</h1>
<hr>
<s:form action="customer.action" method="post" >
<s:textfield name="Name" key="label.name" size="40" />
<s:textfield name="Age" key="label.age" size="40" />
<s:textfield name="Email" key="label.email" size="40" />
<s:textarea name="Message" key="label.msg" rows="10" cols="40" ></s:textarea>
<s:submit key="label.customer" align="center" />
</s:form>
</body>
</html>
success.jsp
create a user's success page.
<%@ 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>success</title>
</head>
<body bgcolor="lightblue">
<center><h3>
Hello! <s:property value="name"/></h3></center>
</body>
</html>
ContactAction.java
create a Action java class.
package in.blog.webideaworld;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ContactAction extends ActionSupport {
private String name;
private int age;
private String email;
private String message;
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() throws Exception{
return SUCCESS;
}
}
ContactAction-validation.xml
Create validators in XML file and the format for the validatiors xml file is <ActionClassName>-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>
<field name="Name">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
<field-validator type="regex">
<param name="expression">[a-zA-Z]{2,20}</param>
<message key="errors.invalid" />
</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">100</param>
<message key="errors.agerange" />
</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="Message">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
<field-validator type="stringlength">
<param name="minLength">4</param>
<param name="maxLength">200</param>
<message key="errors.msgrange"/>
</field-validator>
</field>
</validators>
ApplicationResources.properties
label.name=Name
label.age=Age
label.email=Email
label.msg=Message
label.customer=Send
errors.invalid=${getText(fieldName)} is invalid.
errors.required=${getText(fieldName)} is required.
errors.agerange=Age must be between ${min} and ${max}, current value is ${Age}.
errors.msgrange=Message length must be between ${minLength} to ${maxLength} characters.
struts.xml
set result name="success" for success.jsp and name="input" for contactus.jsp for validation messages to show up on input page.
<?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.custom.i18n.resources" value="ApplicationResources" />
<package name="default" namespace="/" extends="struts-default">
<action name="customer" class="in.blog.webideaworld.ContactAction" method="execute" >
<result name="success">success.jsp</result>
<result name="input">contactus.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>StrutsValidate</display-name>
<welcome-file-list>
<welcome-file>contactus.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Download Code
View in Browser:
checking validation without input values:
checking validation with input values:
success page:
More Struts Topics :
- How to Set Struts2 Environment for Eclipse with Oracle 10g Express Edition
- Simple struts2 program with Eclipse & Apache Tomcat Server
- Login Using Struts2 and Eclipse
- Database Access using Struts2,Eclipse and Oracle
- Basic form Validation using Struts2 Framework with validate() method and ActionSupport class
- Validation using Struts2 Framework and XML file on Radio button and DropDownList with Eclipse
- Validation using Struts2 Framework and XML file with Eclipse Indigo
- AJAX validation using Struts2 Framework and XML file with Eclipse Indigo
- Session Tracking using SessionAware Interface with Struts2, Oracle Database and Eclipse IDE
- Action Wildcards with Struts2
- How to Configure methods in Action mappings with or without Wildcards using Struts2
- Store and Retrieve Numeric, Alphanumeric and Blob (Image) data with Struts2, Oracle 10g XE and Eclipse Indigo as IDE
- How to use Struts2 Iterator tag to get Users Details with Oracle 10g XE and Eclipse
- How to iterate user details through ArrayList with Iterator tag in Struts2 using Oracle 10g XE and Eclipse
- How to upload Multiple Files- Images (blob data) with enctype="multipart/form-data" in Oracle Database using Struts2, JDBC and Eclipse IDE
No comments:
Post a Comment