Process of verifing the format & correctness of the html/jsp form data like required field are typed or not.
There are two types of validation in any application.
- client side validation.(using javascript .js)
- server side validation.(using server side technology like servlet,jsp)
Struts based server side validation
- using programmatic approach using validate(—-) of formBean class.
- using Declerative approach using validator f/w plugin.
Server side validation example
When ever we deploy this program in any server this program we used programmatic validation logic those validation logic written in SubmitForm.java files under of Validate methods.with the help of Application.resources.properties file this generate any errors messages again with the help of
Example deploy the application and we input the given field in browser if forget to fill any singles field and click submit button then result display enter those field.
WelcomeStruts.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <html:html locale="true"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>submit</title> <html:base> <center> <h1>Example Submit Page</h1> </center> <html:errors> <html:form action="submit.do"> </html:form></html:errors> <table align="center" width="500" cellspacing="10"> <tbody> <tr> <td align="right">Last Name:</td> <td> <html:text property="lastName"></html:text></td> </tr> <tr> <td align="right">Address:</td> <td> <html:textarea property="address"></html:textarea></td> </tr> <tr> <td align="right">Sex:</td> <td><html:radio property="sex" value="M">Male <html:radio property="sex" value="F">Female </html:radio></html:radio></td> </tr> <tr> <td align="right">Married:</td> <td> <html:checkbox property="married"></html:checkbox></td> </tr> <tr> <td align="right">Age:</td> <td><html:select property="age"> <html:option value="a">0-19</html:option> <html:option value="b">20-49</html:option> <html:option value="c">50-</html:option> </html:select></td> </tr> <tr> <td colspan="2" align="center"><html:submit></html:submit></td> </tr> </tbody> </table> <center> <h3> <logic:present name="lastName" scope="request"> Hello <logic:equal name="submitForm" property="age" value="a"> young </logic:equal> <logic:equal name="submitForm" property="age" value="c"> old </logic:equal> <bean:write name="lastName" scope="request"> </bean:write></logic:present> </h3> </center> </html:base></html:html> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!--?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"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>welcomeStruts.jsp</welcome-file> </welcome-file-list> </web-app> |
struts-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!--?xml version="1.0" encoding="UTF-8" ?--> <struts-config> <form-beans> <form-bean name="SubmitForm" type="com.myapp.struts.SubmitForm"> </form-bean></form-beans> <global-exceptions> </global-exceptions> <global-forwards> <forward name="welcome" path="/Welcome.do"> </forward></global-forwards> <action-mappings> <action input="/welcomeStruts.jsp" name="SubmitForm" path="/submit" scope="request" type="com.myapp.struts.SubmitAction"> <forward name="success" path="/welcomeStruts.jsp"> <forward name="failure" path="/welcomeStruts.jsp"> </forward></forward></action> </action-mappings> <controller processorclass="org.apache.struts.tiles.TilesRequestProcessor"> <message-resources parameter="com/myapp/struts/ApplicationResource"> </message-resources></controller></struts-config> |
ApplicationResources.property
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | errors.header= <h4>Validation Error(s)</h4> <ul> errors.footer=</ul> <hr> error.lastName= <li>Enter your last name error.address=</li> <li>Enter your address error.sex=</li> <li>Enter your sex error.age=</li> <li>Enter your age </li> |
SubmitForm.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; public class SubmitForm extends org.apache.struts.action.ActionForm { private String lastName = null; // default value public String getLastName() { return (this.lastName); } public void setLastName(String lastName) { this.lastName = lastName; } /* Address */ private String address = null; public String getAddress() { return (this.address); } public void setAddress(String address) { this.address = address; } /* Sex */ private String sex = null; public String getSex() { return (this.sex); } public void setSex(String sex) { this.sex = sex; } /* Married status */ private String married = null; public String getMarried() { return (this.married); } public void setMarried(String married) { this.married = married; } /* Age */ private String age = null; public String getAge() { return (this.age); } public void setAge(String age) { this.age = age; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // Check for mandatory data ActionErrors errors = new ActionErrors(); if (lastName == null || lastName.equals("")) { errors.add("Last Name", new ActionError("error.lastName")); } if (address == null || address.equals("")) { errors.add("Address", new ActionError("error.address")); } if (sex == null || sex.equals("")) { errors.add("Sex", new ActionError("error.sex")); } if (age == null || age.equals("")) { errors.add("Age", new ActionError("error.age")); } return errors; } } |
SubmitAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; public class SubmitAction extends org.apache.struts.action.Action { private final static String SUCCESS = "success"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { SubmitForm f = (SubmitForm) form; // get the form bean // and take the last name value String lastName = f.getLastName(); // Translate the name to upper case //and save it in the request object request.setAttribute("lastName", lastName.toUpperCase()); // Forward control to the specified success target return (mapping.findForward("success")); } } |