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>Struts2.3_ProgramaticValidations</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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>
<include file="struts-default"></include>
<package name="default" extends="struts-default">
<action name="customer" class="com.usr.customer.action.Customer"
method="addCustomer">
<interceptor-ref name="basicStack"></interceptor-ref>
<interceptor-ref name="validation"></interceptor-ref>
<interceptor-ref name="workflow"></interceptor-ref>
<result name="success">/CustomerSuccess.jsp</result>
<result name="input">/customer.jsp</result>
</action>
</package>
</struts>
struts.properties
struts.custom.i18n.resources=messages
messages.properties
title=Struts2 Programatic validations
name=Name
age=Age
email=Email
telephone=Telephone
label.add.customer=Add Customer
errors.required=Name is required
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>Insert title here</title>
</head>
<body>
<s:a href="customer.jsp">Add Customer</s:a>
</body>
</html>
Customer.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>Insert title here</title>
<s:head/>
</head>
<body>
<h2><s:text name="title"></s:text></h2>
<s:form action="customer" method="post">
<s:textfield name="name" key="name"></s:textfield>
<s:textfield name="age" key="age"></s:textfield>
<s:textfield name="email" key="email"></s:textfield>
<s:textfield name="telephone" key="telephone"></s:textfield>
<s:submit key="label.add.customer"></s:submit>
</s:form>
</body>
</html>
CustomerSuccess.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>Insert title here</title>
</head>
<body>
Customer Added Successfully.............
<hr>
<hr>
Name:
<s:property value="name" />
Age:
<s:property value="age" />
Email:
<s:property value="email" />
Telephone:
<s:property value="telephone" />
</body>
</html>
Customer.java
package com.usr.customer.action;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionSupport;
public class Customer extends ActionSupport{
private static final long serialVersionUID = 1L;
private String name;
private Integer age;
private String email;
private String telephone;
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;
}
public String addCustomer(){
return "success";
}
Pattern pattern;
Matcher matcher;
public void validate() {
if(name==null||name.trim().equals("")){
this.addFieldError("name",getText("errors.required"));
}
if(age<1 || age>99){
addFieldError("age", "Please enter age in the range of 1 and 99");
}
if(telephone==null||telephone.trim().equals("")){
addFieldError("telephone", "Please Enter Telephone No");
}
String email_pattern= "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern=Pattern.compile(email_pattern);
if(!email.matches(email_pattern)){
addFieldError("email", "Invalid Email Id");
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.