Thursday 9 February 2012

Struts2 AnnotationBasedValidations


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.properties
struts.custom.i18n.resources=messages
messages.properties 
title=Struts2 Annotation Based validations
name=Name
age=Age
email=Email
telephone=Telephone
label.add.customer=Add Customer

errors.required=${getText(fieldName)} is required
errors.range=${getText(fieldName)} is not in the range ${min} and ${max}.
errors.invalid=${getText(fieldName)} is invalid.

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>

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>   

Customer.java
package com.usr.customer.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

public class Customer extends ActionSupport{
private String name;
private int age;
private String email;
private String telephone;
public String getName() {
     return name;
}
@RequiredStringValidator(type=ValidatorType.FIELD,
      message="Mandatory Field",key="errors.required")
public void setName(String name) {
     this.name = name;
}
public int getAge() {
     return age;
}
@IntRangeFieldValidator(type=ValidatorType.FIELD,min="1",max="99",
message="Allowed Age is Between 1 and 99",key="errors.required")
public void setAge(Integer age) {
     this.age = age;
}
public String getEmail() {
     return email;
}
@RequiredStringValidator(type=ValidatorType.FIELD,
message="Mandatory Field",key="errors.required")
@EmailValidator(type=ValidatorType.FIELD,message="invalid email id")
@StringLengthFieldValidator(type=ValidatorType.FIELD,
message="name should contain min of 7 characters and max 15 charcters",
minLength="7",maxLength="15",trim=true)
public void setEmail(String email) {
     this.email = email;
}
public String getTelephone() {
     return telephone;
}
@RequiredStringValidator(type=ValidatorType.FIELD,
message="Mandatory Field",key="errors.required")
public void setTelephone(String telephone) {
     this.telephone = telephone;
}
public String addCustomer(){
     return "success";
}
}


 
 

 
 

 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.