In this example, We will discuss about the Tabular Input validation using struts2.2.1.
In this example,We validate the input fields, The Tabular Inputs are used to take the multiple inputs from a form like cart.
The Interceptor and action mapping is in the struts.xml file.
1-index.jsp
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags"%>< html>< head>< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">< title>Tabular Input</title>< s:head /></ head>< body>< s:form action="update" method="post"> <s:textfield label="Name" name="name" /> <s:textfield label="Age" name="age" /> <s:textfield label="Height" name="height" /> <s:submit /></ s:form></ body></ html> |
2-PersonAction.java
|
package roseindia;import java.util.List;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.validator.annotations.Validation;public class PersonAction extends ActionSupport { private static final long serialVersionUID = 1L; private Integer id; private String name; private Integer age; private Integer height; public Integer getId() { return id;} public void setId(Integer id) { this.id = id;} 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 Integer getHeight() { return height;} public void setHeight(Integer height) { this.height = height;} public String execute() throws Exception { return SUCCESS;} } |
3-success.jsp
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><! 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=UTF-8">< title>Insert title here</title></ head>< body>< h1>Data Inserted</h1></ body></ html> |
4 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>< constant name="struts.enable.DynamicMethodInvocation" value="false" />< constant name="struts.devMode" value="false" /> <package name="default" extends="struts-default" namespace="/" > <action name="update" class="roseindia.PersonAction"> <result name="input">index.jsp</result> <result name="success">/success.jsp</result> </action> </package> </struts> |
index.jsp



success.gif
