Tuesday, 3 December 2013

struts2 integer validation

Today i am sharing my demo example for struts2 validation for integer value only.suppose you have a requirement to pass only integer value in the server so these example will help you a lot.

I am going to demonstrate the example step by step.

1. Create your default web project directory structure in your IDE.
2. Open the web.xml file and do filterdispatcher entry in it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">

 <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>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>


3. Now create a struts.xml file in your src folder.

<!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" />
 <package name="struts2" extends="struts-default" namespace="/">
 <action name="integerAction">
 <result name="success">/integer.jsp</result>
 <result name="input">/integer.jsp</result>
 </action>
 <action name="displayInteger">
 <result name="success">/integer.jsp</result>
 </action>
 </package>
</struts>
 
 4. Now create the validation xml file . please remember that validation xml file name should be same as your action name.here my validation xml file name is “IntegerValidation-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>
 <validator type="regex">
 <param name="fieldName">integerValue</param>
 <param name="expression">\d+</param>
 <message>Only Numberic Value Allowed</message>
 </validator>
</validators>

 5.Now create a one Struts2 Action class named “IntegerValidation”.

package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
public class IntegerValidation extends ActionSupport {

 private String integerValue;
 public String execute() {
 System.out.println("I m calling");
 return SUCCESS;
 }

 public String getIntegerValue() {
 return integerValue;
 }

 public void setIntegerValue(String integerValue) {
 this.integerValue = integerValue;
 }
}
 
 6. Now create the one jsp file named “integer.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">
 <s:head theme="ajax" />
 <title>Integer Validation Test</title>
 </head>
 <body>
 <s:fielderror />
 <s:form  action="integerAction" method="post" validate="true" namespace="/">
 <s:textfield key="integerValue" label="Zip Code" labelposition="left"
 cssStyle="size: 15" required="true" name="integerValue"></s:textfield>
 <s:submit value="Validate Integer Only"/>
 </s:form>
 </body>
</html>

Now deploy your application and run the following URL at your browser

http://localhost:8080/YourProjectName/displayInteger

If you have any problem in this example so please free feel to ask.
Thanks to visit.

No comments:

Post a Comment