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.ADS_TO_REPLACE_1
1-index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>ADS_TO_REPLACE_2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags"%>ADS_TO_REPLACE_3 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">ADS_TO_REPLACE_4 <title>Tabular Input</title> <s:head /> </head>ADS_TO_REPLACE_5 <body> <s:form action="update" method="post"> <s:textfield label="Name" name="name" />ADS_TO_REPLACE_6 <s:textfield label="Age" name="age" /> <s:textfield label="Height" name="height" /> <s:submit />ADS_TO_REPLACE_7 </s:form> </body> </html> |
2-PersonAction.javaADS_TO_REPLACE_8
package roseindia; import java.util.List; import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_9 import com.opensymphony.xwork2.validator.annotations.Validation; public class PersonAction extends ActionSupport { private static final long serialVersionUID = 1L;ADS_TO_REPLACE_10 private Integer id; private String name; private Integer age;ADS_TO_REPLACE_11 private Integer height; public Integer getId() { return id;ADS_TO_REPLACE_12 } public void setId(Integer id) { this.id = id;ADS_TO_REPLACE_13 } public String getName() { return name;ADS_TO_REPLACE_14 } public void setName(String name) { this.name = name;ADS_TO_REPLACE_15 } public Integer getAge() { return age;ADS_TO_REPLACE_16 } public void setAge(Integer age) { this.age = age;ADS_TO_REPLACE_17 } public Integer getHeight() { return height;ADS_TO_REPLACE_18 } public void setHeight(Integer height) { this.height = height;ADS_TO_REPLACE_19 } public String execute() throws Exception { return SUCCESS;ADS_TO_REPLACE_20 } } |
3-success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"ADS_TO_REPLACE_21 pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">ADS_TO_REPLACE_22 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">ADS_TO_REPLACE_23 <title>Insert title here</title> </head> <body>ADS_TO_REPLACE_24 <h1>Data Inserted</h1> </body> </html> |
4 struts.xmlADS_TO_REPLACE_25
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"ADS_TO_REPLACE_26 "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />ADS_TO_REPLACE_27 <constant name="struts.devMode" value="false" /> <package name="default" extends="struts-default" namespace="/" > <action name="update" class="roseindia.PersonAction">ADS_TO_REPLACE_28 <result name="input">index.jsp</result> <result name="success">/success.jsp</result> </action>ADS_TO_REPLACE_29 </package> </struts> |
index.jsp
ADS_TO_REPLACE_30
success.gifADS_TO_REPLACE_31
Advertisements
Ads
Ads