Thursday 9 February 2012

Struts2 Xml Validations




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 xml 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" /><br>
       Age:
       <s:property value="age" /><br>
       Email:
       <s:property value="email" /><br>
       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;

public class Customer extends ActionSupport{
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";
}
}

Customer-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
       <field name="name">
              <field-validator type="requiredstring">
                     <param name="trim">true</param>
                     <message key="errors.required" />
              </field-validator>
       </field>
       <field name="age">
              <field-validator type="required">
                     <message key="errors.required"></message>
              </field-validator>
              <field-validator type="int">
                     <param name="min">1</param>
                     <param name="max">100</param>
                     <message key="errors.range"></message>
              </field-validator>
       </field>
       <field name="email">
              <field-validator type="requiredstring">
                     <message key="errors.required"></message>
              </field-validator>
              <field-validator type="email">
                     <message key="errors.invalid"></message>
              </field-validator>
       </field>
       <field name="telephone">
              <field-validator type="requiredstring">
                     <message key="errors.required"></message>
              </field-validator>
       </field>
</validators>       

 
 


 

No comments:

Post a Comment

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