The Attached code will provide you a complete code which has used all
types of forms available in struts1(except than action form).
In this single application I tried to use all types of Struts1 Form with basic validation using Struts1 Validation Framework and custom validations which are registerd with validation framework.
Hope it will be useful:
1. Directory structure
2. Jars and tlds Required
Presentaion Files(JSPs)
1.EmployerDetails.jsp
-----------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Employer Details</title>
</head>
<body><center>
<table align="center" bgcolor="#ffffff">
<tr><td colspan="3" align="center"><h2>ValidatorActionForm</h2></td></tr>
<tr>
<td colspan="4" align="center"><bean:message key="lable.employer.detail"/></td>
</tr>
<tr></tr>
<tr><th></th><th>Name</th><th>Year Of Exp</th></tr>
<html:form action="/employer">
<tr>
<td><bean:message key="lable.employer.first"/></td>
<td><html:text property="fename"/></td>
<td><html:text property="fnoyrs"/></td>
<td><html:errors property="fename"/></td>
</tr>
<tr>
<td><bean:message key="lable.employer.second"/></td>
<td><html:text property="sename"/></td>
<td><html:text property="snoyrs"/></td>
<td><html:errors property="sename"/></td>
</tr>
<tr>
<td><bean:message key="lable.employer.third"/></td>
<td><html:text property="tename"/></td>
<td><html:text property="tnoyrs"/></td>
<td><html:errors property="tename"/></td>
</tr>
<tr>
<td align="center" colspan="4">
<html:submit value="Submit"/>
</td>
</tr>
</html:form>
</table>
</center>
</body>
</html>
2. enquiry.jsp
----------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="in.ibm.strurts.*"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>
<table align="center">
<tr><td colspan="3" align="center"><h2>ValidatorForm</h2></td></tr>
<tr><td colspan="3" align="center"><h1>
<bean:message key="Lable.enquiry.Header"/></h1></td></tr>
<html:form action="/info">
<tr>
<td>Name</td>
<td><html:text property="name"/></td>
<td><html:errors property="name"/></td>
</tr>
<tr>
<td>Mobile No</td>
<td><html:text property="phone"/></td>
<td><html:errors property="phone"/></td>
</tr>
<tr>
<td>Batch</td>
<td><html:text property="batch"/></td>
<td><html:errors property="batch"/></td>
</tr>
<tr>
<td>DateOfBirth</td>
<td><html:text property="dob"/></td>
<td><html:errors property="dob"/></td>
</tr>
<tr>
<td>Email</td>
<td><html:text property="email"/></td>
<td><html:errors property="email"/></td>
</tr>
<tr>
<td>Gender</td>
<td>Male<html:radio property="gender" value="Male"/>
Female<html:radio property="gender" value="Female"/></td>
<td><html:errors property="gender"/></td>
</tr>
<tr>
<td>City</td>
<td><html:select property="city">
<html:option value="">-----Select------</html:option>
<html:option value="Bangalore">Bangalore</html:option>
<html:option value="Pune">Pune</html:option>
<html:option value="Kolkata">Kolkata</html:option>
<html:option value="Mumbai">Mumbai</html:option>
</html:select></td>
<td><html:errors property="city"/></td>
</tr>
<tr>
<td>Subjects</td>
<td>JSF<html:checkbox property="sub" value="JSF"></html:checkbox>
JSP<html:checkbox property="sub" value="JSP"></html:checkbox>
Java<html:checkbox property="sub" value="Java"></html:checkbox>
Struts<html:checkbox property="sub" value="Struts"></html:checkbox></td>
<td><html:errors property="sub"/></td>
</tr>
<tr>
<td>Query Relevant Topic</td>
<td><html:select property="top" multiple="true">
<html:option value="">-----Select------</html:option>
<html:option value="Java">Java</html:option>
<html:option value="DataBase">DataBase</html:option>
<html:option value="Location">Location</html:option>
<html:option value="teacher">teacher</html:option>
</html:select></td>
<td><html:errors property="top"/></td>
</tr>
<tr>
<td>Write Your Query</td>
</tr>
<tr>
<td colspan="3" align="left"><html:textarea property="query"></html:textarea></td>
</tr>
<tr><td colspan="3" align="center"><html:submit value="register"></html:submit>
</tr>
</html:form>
</table>
</body>
</html>
3. expectations.jsp
------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="in.ibm.strurts.*"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Expectation</title>
</head>
<body>
<table align="center">
<tr><td colspan="3" align="center"><h2>DynaValidatorActionForm</h2></td></tr>
<tr><td colspan="3" align="center"><h1>
<bean:message key="Lable.expectations.Header"/></h1></td></tr>
<html:form action="/expectations">
<tr>
<td>Expected Salary</td>
<td><html:text property="esalary"/></td>
<td><html:errors property="esalary"/></td>
</tr>
<tr>
<td>Location</td>
<td><html:text property="eloc"/></td>
<td><html:errors property="eloc"/></td>
</tr>
<tr>
<td>Project Type</td>
<td><html:text property="ptype"/></td>
<td><html:errors property="ptype"/></td>
</tr>
<tr>
<td colspan="3" align="center"><html:submit value="Done"></html:submit>
</tr>
</html:form>
</table>
</body>
</html>
4. home.jsp
-----------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<!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>
<html:link forward="enquiry"><h1>enquiry</h1> </html:link><br>
</body>
</html>
5. logoff.jsp
--------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic" %>
<!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>
<html:errors/>
<center>
<h1>Congratulations, You have learned Struts1 Form Types very well</h1>
</center>
</body>
</html>
6. PersonalDetail.jsp
--------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Personal Details</title>
</head><center><tr><td colspan="3" align="center"><h2>ValidatorForm</h2></td></tr>
<body>
<bean:message key="lable.personal.detail"/><br>
<html:form action="/submit">
<bean:message key="lable.personal.mname"/>
<html:text property="mname"/><html:errors property="mname"/> <br>
<bean:message key="lable.personal.salary"/>
<html:text property="salary"/><html:errors property="salary"/> <br>
<bean:message key="lable.personal.yoe"/>
<html:text property="yoe"/><html:errors property="yoe"/> <br>
<bean:message key="lable.personal.age"/>
<html:text property="age"/><html:errors property="age"/> <br>
<html:submit value="Submit"/>
</html:form>
</center>
</body>
</html>
7.QualificationDetails.jsp
--------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Qualification Details</title>
</head>
<body><center>
<table align="center" bgcolor="#ffffff">
<tr><td colspan="3" align="center"><h2>ValidatorActionForm</h2></td></tr>
<tr><td colspan="6" align="center"><bean:message key="lable.qualification.detail"/></td></tr>
<tr></tr>
<tr><th></th><th>Percentage</th><th>Passing Year</th><th>College Name</th><th>Univercity</th></tr>
<html:form action="/qualifiction">
<tr>
<td><bean:message key="lable.qualification.ssc"/></td>
<td><html:text property="sscper"/></td>
<td><html:text property="sscyop"/></td>
<td><html:text property="ssccol"/></td>
<td><html:text property="sscuni"/></td>
<td><html:errors property="sscper"/></td>
</tr>
<tr>
<td><bean:message key="lable.qualification.pu"/></td>
<td><html:text property="puper"/></td>
<td><html:text property="puyop"/></td>
<td><html:text property="pucol"/></td>
<td><html:text property="puuni"/></td>
<td><html:errors property="puper"/></td>
</tr>
<tr>
<td><bean:message key="lable.qualification.grad"/></td>
<td><html:text property="gradper"/></td>
<td><html:text property="gradyop"/></td>
<td><html:text property="gradcol"/></td>
<td><html:text property="graduni"/></td>
<td><html:errors property="gradper"/></td>
</tr>
<tr>
<td><bean:message key="lable.qualification.pg"/></td>
<td><html:text property="pgper"/></td>
<td><html:text property="pgyop"/></td>
<td><html:text property="pgcol"/></td>
<td><html:text property="pguni"/></td>
<td><html:errors property="pgper"/></td>
</tr>
<tr>
<td align="center" colspan="6">
<html:submit value="Submit"/>
</td>
</tr>
</html:form>
</table>
</center>
</body>
</html>
8. Skills.jsp
----------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="in.ibm.strurts.*"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Skills</title>
</head>
<body>
<table align="center">
<tr><td colspan="3" align="center"><h2>DynaValidatorActionForm</h2></td></tr>
<tr><td colspan="3" align="center">
<h1><bean:message key="Lable.skill.Header"/></h1></td></tr>
<html:form action="/skills">
<tr>
<td>PrimarySkills</td>
<td><html:text property="pskills"/></td>
<td><html:errors property="pskills"/></td>
</tr>
<tr>
<td>Secondary Skills</td>
<td><html:text property="sskills"/></td>
<td><html:errors property="sskills"/></td>
</tr>
<tr>
<td>Web Skills</td>
<td><html:text property="wskills"/></td>
<td><html:errors property="wskills"/></td>
</tr>
<tr>
<td colspan="2" align="center">Your Hobbies and Interests</td>
</tr>
<tr>
<td colspan="3" align="center"><html:textarea property="hobby"></html:textarea></td>
</tr>
<tr><td colspan="3" align="center"><html:submit value="Submit"></html:submit>
</tr>
</html:form>
</table>
</body>
</html>
COnfiguration Files(XMLs)
8. struts-config.xml
-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="perForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="mname" type="java.lang.String"/>
<form-property name="salary" type="java.lang.String"/>
<form-property name="yoe" type="java.lang.String"/>
<form-property name="age" type="java.lang.String"/>
</form-bean>
<form-bean name="skExpForm" type="org.apache.struts.validator.DynaValidatorActionForm">
<form-property name="pskills" type="java.lang.String"/>
<form-property name="sskills" type="java.lang.String"/>
<form-property name="wskills" type="java.lang.String"/>
<form-property name="hobby" type="java.lang.String"/>
<form-property name="esalary" type="java.lang.String"/>
<form-property name="eloc" type="java.lang.String"/>
<form-property name="ptype" type="java.lang.String"/>
</form-bean>
<form-bean name="enqForm" type="in.ibm.strurts.EnquiryForm"/>
<form-bean name="qualempForm" type="in.ibm.strurts.Qualification_EmployerForm"/>
</form-beans>
<global-exceptions>
<exception key="exception.error" type="java.lang.Exception" path="/logoff.jsp"/>
</global-exceptions>
<global-forwards>
<forward name="enquiry" path="/enquiry.jsp"/>
<forward name="logoff" path="/logoff.jsp"/>
</global-forwards>
<action-mappings>
<action path="/info" name="enqForm" type="in.ibm.strurts.EnquiryAction" validate="true" scope="request" input="/enquiry.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/PersonalDetail.jsp"/>
</action>
<action path="/submit" name="perForm" type="in.ibm.strurts.PersonalAction" validate="true" scope="request" input="/PersonalDetail.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/QualificationDetails.jsp"/>
</action>
<action path="/qualifiction" name="qualempForm" type="in.ibm.strurts.QualificationAction" validate="true" scope="request" input="/QualificationDetails.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/EmployerDetails.jsp"/>
</action>
<action path="/employer" name="qualempForm" type="in.ibm.strurts.EmployerAction" validate="true" scope="request" input="/EmployerDetails.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/Skills.jsp"/>
</action>
<action path="/skills" name="skExpForm" type="in.ibm.strurts.SkillsAction" validate="true" scope="request" input="/Skills.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/expectations.jsp"/>
</action>
<action path="/expectations" name="skExpForm" type="in.ibm.strurts.ExpectationAction" validate="true" scope="request" input="/expectations.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/logoff.jsp"/>
</action>
</action-mappings>
<message-resources parameter="in.ibm.strurts.messages"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml" property="pathnames"/>
</plug-in>
</struts-config>
9. validations.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation
//DTD Commons Validator Rules Configuration 1.0.1//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0_1.dtd">
<form-validation>
<formset>
<form name="/employer">
<field property="fename" depends="required">
<arg0 key="EMployerName" resource="false"/>
</field>
<field property="sename" depends="required">
<arg0 key="EMployerName" resource="false"/>
</field>
<field property="tename" depends="required">
<arg0 key="EMployerName" resource="false"/>
</field>
</form>
<form name="/skills">
<field property="pskills" depends="required">
<arg0 key="Primary Skill" resource="false"/>
</field>
<field property="sskills" depends="required">
<arg0 key="Secondary Skill" resource="false"/>
</field>
<field property="wskills" depends="required">
<arg0 key="Web Skill" resource="false"/>
</field>
</form>
<form name="/expectations">
<field property="esalary" depends="required">
<arg0 key="Expected Salary Field" resource="false"/>
</field>
<field property="ptype" depends="required">
<arg0 key="Project Type Field" resource="false"/>
</field>
</form>
<form name="/qualifiction">
<field property="sscper" depends="required">
<arg0 key="10th Percentage " resource="false"/>
</field>
<field property="puper" depends="required">
<arg0 key="12th Percentage" resource="false"/>
</field>
<field property="gradper" depends="required">
<arg0 key="Graduation Percentage" resource="false"/>
</field>
</form>
<form name="enqForm">
<field property="name" depends="required,maxlength,minlength">
<arg0 key="lable.user" resource="true"/>
<arg1 key="${var:minlength}" resource="false"/>
<arg2 key="${var:maxlength}" resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>10</var-value>
</var>
</field>
<field property="email" depends="required,email">
<arg0 key="lable.email" resource="true"/>
<arg1 key="lable.email" resource="true"/>
</field>
<field property="phone" depends="long,mask">
<arg0 key="lable.phone" resource="true"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]{10}$</var-value>
</var>
</field>
<field property="sub" depends="lengthRequired">
<arg0 key="lable.subject" resource="true"/>
</field>
<field property="dob" depends="required,date">
<arg0 key="Date" resource="false"/>
<var>
<var-name>datePattern</var-name>
<var-value>dd/MM/yyyy</var-value>
</var>
</field>
</form>
<form name="perForm">
<field property="mname" depends="required">
<arg0 key="lable.personal.mname" resource="true"/>
</field>
<field property="salary" depends="required,float">
<arg0 key="lable.personal.salary" resource="true"/>
</field>
<field property="age" depends="required,integer,intRange">
<arg0 key="lable.personal.age" resource="true"/>
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 name="intRange" key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-value>18</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>65</var-value>
</var>
</field>
<field property="yoe" depends="required,integer,range">
<arg0 key="lable.personal.yoe" resource="true"/>
<arg1 name="range" key="${var:min}" resource="false"/>
<arg2 name="range" key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-value>3</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>10</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
10. 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>FormElementWithValFram</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hi</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hi</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
11. validator-rules.xml
------------------------------
Download from net ( GOOGLE-struts1 validation-rules.xml)
JAVA CODE
12. EmployerAction
--------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class EmployerAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Employer Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
13. EnquiryAction
------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class EnquiryAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println(req.getAttribute("enqForm"));
ActionForward af= mapping.findForward("success");
return af;
}
}
14. EnquiryForm
-----------------------
package in.ibm.strurts;
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.validator.ValidatorForm;
public class EnquiryForm extends ValidatorForm {
private String name;
private String batch;
private String gender;
private String city;
private String[] sub;
private String[] top;
private String query;
private String email;
private long phone;
private String dob;
/*@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors err=new ActionErrors();
if(!batch.equals("b33")){
err.add("batch",new ActionError("error.batch.required"));
}
return err;
}*/
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBatch() {
return batch;
}
public void setBatch(String batch) {
this.batch = batch;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String[] getSub() {
return sub;
}
public void setSub(String[] sub) {
this.sub = sub;
}
public String[] getTop() {
return top;
}
public void setTop(String[] top) {
this.top = top;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name="sushil";
this.city="Bangalore";
}
}
15. EnquirySubjectValidation
----------------------------------------
package in.ibm.strurts;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.*;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
public class EnquirySubjectValidation {
public void validateSubject(ActionErrors err,HttpServletRequest req){
System.out.println("in Custom Validation");
String[] sub=req.getParameterValues("sub");
System.out.println(sub.length);
if(sub.length>2){
err.add("sub",new ActionError("error.sub.length"));
}
}
}
16. ExpectationAction
-----------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ExpectationAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Employer Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
17. IllegalException
------------------------
package in.ibm.strurts;
public class IllegalException extends Exception {
}
18. PersonalAction
-------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class PersonalAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println(req.getAttribute("perForm"));
ActionForward af= mapping.findForward("success");
return af;
}
}
19. Qualification_EmployerForm
------------------------------------------
package in.ibm.strurts;
import org.apache.struts.validator.ValidatorActionForm;
public class Qualification_EmployerForm extends ValidatorActionForm {
private String fename;
private String fnoyrs;
private String sename;
private String snoyrs;
private String tename;
private String tnoyrs;
private String sscper;
private String sscyop;
private String ssccol;
private String sscuni;
private String puper;
private String puyop;
private String pucol;
private String puuni;
private String gradper;
private String gradyop;
private String gradcol;
private String graduni;
private String pgper;
private String pgyop;
private String pgcol;
private String pguni;
public String getFename() {
return fename;
}
public void setFename(String fename) {
this.fename = fename;
}
public String getFnoyrs() {
return fnoyrs;
}
public void setFnoyrs(String fnoyrs) {
this.fnoyrs = fnoyrs;
}
public String getSename() {
return sename;
}
public void setSename(String sename) {
this.sename = sename;
}
public String getSnoyrs() {
return snoyrs;
}
public void setSnoyrs(String snoyrs) {
this.snoyrs = snoyrs;
}
public String getTename() {
return tename;
}
public void setTename(String tename) {
this.tename = tename;
}
public String getTnoyrs() {
return tnoyrs;
}
public void setTnoyrs(String tnoyrs) {
this.tnoyrs = tnoyrs;
}
public String getSscper() {
return sscper;
}
public void setSscper(String sscper) {
this.sscper = sscper;
}
public String getSscyop() {
return sscyop;
}
public void setSscyop(String sscyop) {
this.sscyop = sscyop;
}
public String getSsccol() {
return ssccol;
}
public void setSsccol(String ssccol) {
this.ssccol = ssccol;
}
public String getSscuni() {
return sscuni;
}
public void setSscuni(String sscuni) {
this.sscuni = sscuni;
}
public String getPuper() {
return puper;
}
public void setPuper(String puper) {
this.puper = puper;
}
public String getPuyop() {
return puyop;
}
public void setPuyop(String puyop) {
this.puyop = puyop;
}
public String getPucol() {
return pucol;
}
public void setPucol(String pucol) {
this.pucol = pucol;
}
public String getPuuni() {
return puuni;
}
public void setPuuni(String puuni) {
this.puuni = puuni;
}
public String getGradper() {
return gradper;
}
public void setGradper(String gradper) {
this.gradper = gradper;
}
public String getGradyop() {
return gradyop;
}
public void setGradyop(String gradyop) {
this.gradyop = gradyop;
}
public String getGradcol() {
return gradcol;
}
public void setGradcol(String gradcol) {
this.gradcol = gradcol;
}
public String getGraduni() {
return graduni;
}
public void setGraduni(String graduni) {
this.graduni = graduni;
}
public String getPgper() {
return pgper;
}
public void setPgper(String pgper) {
this.pgper = pgper;
}
public String getPgyop() {
return pgyop;
}
public void setPgyop(String pgyop) {
this.pgyop = pgyop;
}
public String getPgcol() {
return pgcol;
}
public void setPgcol(String pgcol) {
this.pgcol = pgcol;
}
public String getPguni() {
return pguni;
}
public void setPguni(String pguni) {
this.pguni = pguni;
}
}
20. QualificationAction
--------------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class QualificationAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Qualification Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
21. SkillsAction
----------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class SkillsAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Employer Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
PROPERTIES FILE
22. messages.properties
-----------------------------
#lables
lable.user=Username
lable.email=Email
lable.phone=Mobile no
Lable.enquiry.Header=Fill Enquiry Form
lable.subject=Subject
lable.personal.detail=Personal Deatils are required
lable.personal.yoe= Year of Experience
lable.personal.salary=Salary
lable.personal.mname=Mother's Name
lable.personal.age=Age
lable.qualification.detail=<h1>Qualification Details</h1>
lable.qualification.ssc=10th details
lable.qualification.pu=12th details
lable.qualification.grad=Graduation details
lable.qualification.pg=PG details
Lable.expectations.Header=<h1>Your Expectations</h1>
Lable.skill.Header=<h1>Your Skills</h1>
lable.employer.detail=<h1>Previous Employer Details</h1>
lable.employer.first=<h3>First EMployer</h1>
lable.employer.second=<h3>Second EMployer</h1>
lable.employer.third=<h3>Third EMployer</h1>
#errors
errors.header=<font size="4"><UL>
errors.prefix=<LI><span style="color: red">
errors.suffix=</span></LI>
errors.footer=</UL></font>
errors.required= {0} is required.
errors.maxlength={0} length should be max {2} character
errors.minlength={0} length should be min {1} character
errors.email={1} is not in proper format
errors.long={0} should be long
errors.invalid={0} should contains 10 digits
error.batch.required=Batch should be B33
error.sub.length= you should select minimum two {0}
errors.date=date pattern should be dd/MM/yyyy
errors.float={0} should be float
errors.integer={0} should be integer.
errors.range={0} should be between {1}-{2}
exception.user=Exception is thrown
---------------------------------------------------------------------------------
In this single application I tried to use all types of Struts1 Form with basic validation using Struts1 Validation Framework and custom validations which are registerd with validation framework.
Hope it will be useful:
1. Directory structure
2. Jars and tlds Required
Source Code
Presentaion Files(JSPs)
1.EmployerDetails.jsp
-----------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Employer Details</title>
</head>
<body><center>
<table align="center" bgcolor="#ffffff">
<tr><td colspan="3" align="center"><h2>ValidatorActionForm</h2></td></tr>
<tr>
<td colspan="4" align="center"><bean:message key="lable.employer.detail"/></td>
</tr>
<tr></tr>
<tr><th></th><th>Name</th><th>Year Of Exp</th></tr>
<html:form action="/employer">
<tr>
<td><bean:message key="lable.employer.first"/></td>
<td><html:text property="fename"/></td>
<td><html:text property="fnoyrs"/></td>
<td><html:errors property="fename"/></td>
</tr>
<tr>
<td><bean:message key="lable.employer.second"/></td>
<td><html:text property="sename"/></td>
<td><html:text property="snoyrs"/></td>
<td><html:errors property="sename"/></td>
</tr>
<tr>
<td><bean:message key="lable.employer.third"/></td>
<td><html:text property="tename"/></td>
<td><html:text property="tnoyrs"/></td>
<td><html:errors property="tename"/></td>
</tr>
<tr>
<td align="center" colspan="4">
<html:submit value="Submit"/>
</td>
</tr>
</html:form>
</table>
</center>
</body>
</html>
2. enquiry.jsp
----------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="in.ibm.strurts.*"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>
<table align="center">
<tr><td colspan="3" align="center"><h2>ValidatorForm</h2></td></tr>
<tr><td colspan="3" align="center"><h1>
<bean:message key="Lable.enquiry.Header"/></h1></td></tr>
<html:form action="/info">
<tr>
<td>Name</td>
<td><html:text property="name"/></td>
<td><html:errors property="name"/></td>
</tr>
<tr>
<td>Mobile No</td>
<td><html:text property="phone"/></td>
<td><html:errors property="phone"/></td>
</tr>
<tr>
<td>Batch</td>
<td><html:text property="batch"/></td>
<td><html:errors property="batch"/></td>
</tr>
<tr>
<td>DateOfBirth</td>
<td><html:text property="dob"/></td>
<td><html:errors property="dob"/></td>
</tr>
<tr>
<td>Email</td>
<td><html:text property="email"/></td>
<td><html:errors property="email"/></td>
</tr>
<tr>
<td>Gender</td>
<td>Male<html:radio property="gender" value="Male"/>
Female<html:radio property="gender" value="Female"/></td>
<td><html:errors property="gender"/></td>
</tr>
<tr>
<td>City</td>
<td><html:select property="city">
<html:option value="">-----Select------</html:option>
<html:option value="Bangalore">Bangalore</html:option>
<html:option value="Pune">Pune</html:option>
<html:option value="Kolkata">Kolkata</html:option>
<html:option value="Mumbai">Mumbai</html:option>
</html:select></td>
<td><html:errors property="city"/></td>
</tr>
<tr>
<td>Subjects</td>
<td>JSF<html:checkbox property="sub" value="JSF"></html:checkbox>
JSP<html:checkbox property="sub" value="JSP"></html:checkbox>
Java<html:checkbox property="sub" value="Java"></html:checkbox>
Struts<html:checkbox property="sub" value="Struts"></html:checkbox></td>
<td><html:errors property="sub"/></td>
</tr>
<tr>
<td>Query Relevant Topic</td>
<td><html:select property="top" multiple="true">
<html:option value="">-----Select------</html:option>
<html:option value="Java">Java</html:option>
<html:option value="DataBase">DataBase</html:option>
<html:option value="Location">Location</html:option>
<html:option value="teacher">teacher</html:option>
</html:select></td>
<td><html:errors property="top"/></td>
</tr>
<tr>
<td>Write Your Query</td>
</tr>
<tr>
<td colspan="3" align="left"><html:textarea property="query"></html:textarea></td>
</tr>
<tr><td colspan="3" align="center"><html:submit value="register"></html:submit>
</tr>
</html:form>
</table>
</body>
</html>
3. expectations.jsp
------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="in.ibm.strurts.*"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Expectation</title>
</head>
<body>
<table align="center">
<tr><td colspan="3" align="center"><h2>DynaValidatorActionForm</h2></td></tr>
<tr><td colspan="3" align="center"><h1>
<bean:message key="Lable.expectations.Header"/></h1></td></tr>
<html:form action="/expectations">
<tr>
<td>Expected Salary</td>
<td><html:text property="esalary"/></td>
<td><html:errors property="esalary"/></td>
</tr>
<tr>
<td>Location</td>
<td><html:text property="eloc"/></td>
<td><html:errors property="eloc"/></td>
</tr>
<tr>
<td>Project Type</td>
<td><html:text property="ptype"/></td>
<td><html:errors property="ptype"/></td>
</tr>
<tr>
<td colspan="3" align="center"><html:submit value="Done"></html:submit>
</tr>
</html:form>
</table>
</body>
</html>
4. home.jsp
-----------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<!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>
<html:link forward="enquiry"><h1>enquiry</h1> </html:link><br>
</body>
</html>
5. logoff.jsp
--------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic" %>
<!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>
<html:errors/>
<center>
<h1>Congratulations, You have learned Struts1 Form Types very well</h1>
</center>
</body>
</html>
6. PersonalDetail.jsp
--------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Personal Details</title>
</head><center><tr><td colspan="3" align="center"><h2>ValidatorForm</h2></td></tr>
<body>
<bean:message key="lable.personal.detail"/><br>
<html:form action="/submit">
<bean:message key="lable.personal.mname"/>
<html:text property="mname"/><html:errors property="mname"/> <br>
<bean:message key="lable.personal.salary"/>
<html:text property="salary"/><html:errors property="salary"/> <br>
<bean:message key="lable.personal.yoe"/>
<html:text property="yoe"/><html:errors property="yoe"/> <br>
<bean:message key="lable.personal.age"/>
<html:text property="age"/><html:errors property="age"/> <br>
<html:submit value="Submit"/>
</html:form>
</center>
</body>
</html>
7.QualificationDetails.jsp
--------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Qualification Details</title>
</head>
<body><center>
<table align="center" bgcolor="#ffffff">
<tr><td colspan="3" align="center"><h2>ValidatorActionForm</h2></td></tr>
<tr><td colspan="6" align="center"><bean:message key="lable.qualification.detail"/></td></tr>
<tr></tr>
<tr><th></th><th>Percentage</th><th>Passing Year</th><th>College Name</th><th>Univercity</th></tr>
<html:form action="/qualifiction">
<tr>
<td><bean:message key="lable.qualification.ssc"/></td>
<td><html:text property="sscper"/></td>
<td><html:text property="sscyop"/></td>
<td><html:text property="ssccol"/></td>
<td><html:text property="sscuni"/></td>
<td><html:errors property="sscper"/></td>
</tr>
<tr>
<td><bean:message key="lable.qualification.pu"/></td>
<td><html:text property="puper"/></td>
<td><html:text property="puyop"/></td>
<td><html:text property="pucol"/></td>
<td><html:text property="puuni"/></td>
<td><html:errors property="puper"/></td>
</tr>
<tr>
<td><bean:message key="lable.qualification.grad"/></td>
<td><html:text property="gradper"/></td>
<td><html:text property="gradyop"/></td>
<td><html:text property="gradcol"/></td>
<td><html:text property="graduni"/></td>
<td><html:errors property="gradper"/></td>
</tr>
<tr>
<td><bean:message key="lable.qualification.pg"/></td>
<td><html:text property="pgper"/></td>
<td><html:text property="pgyop"/></td>
<td><html:text property="pgcol"/></td>
<td><html:text property="pguni"/></td>
<td><html:errors property="pgper"/></td>
</tr>
<tr>
<td align="center" colspan="6">
<html:submit value="Submit"/>
</td>
</tr>
</html:form>
</table>
</center>
</body>
</html>
8. Skills.jsp
----------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="in.ibm.strurts.*"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<!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>Skills</title>
</head>
<body>
<table align="center">
<tr><td colspan="3" align="center"><h2>DynaValidatorActionForm</h2></td></tr>
<tr><td colspan="3" align="center">
<h1><bean:message key="Lable.skill.Header"/></h1></td></tr>
<html:form action="/skills">
<tr>
<td>PrimarySkills</td>
<td><html:text property="pskills"/></td>
<td><html:errors property="pskills"/></td>
</tr>
<tr>
<td>Secondary Skills</td>
<td><html:text property="sskills"/></td>
<td><html:errors property="sskills"/></td>
</tr>
<tr>
<td>Web Skills</td>
<td><html:text property="wskills"/></td>
<td><html:errors property="wskills"/></td>
</tr>
<tr>
<td colspan="2" align="center">Your Hobbies and Interests</td>
</tr>
<tr>
<td colspan="3" align="center"><html:textarea property="hobby"></html:textarea></td>
</tr>
<tr><td colspan="3" align="center"><html:submit value="Submit"></html:submit>
</tr>
</html:form>
</table>
</body>
</html>
COnfiguration Files(XMLs)
8. struts-config.xml
-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="perForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="mname" type="java.lang.String"/>
<form-property name="salary" type="java.lang.String"/>
<form-property name="yoe" type="java.lang.String"/>
<form-property name="age" type="java.lang.String"/>
</form-bean>
<form-bean name="skExpForm" type="org.apache.struts.validator.DynaValidatorActionForm">
<form-property name="pskills" type="java.lang.String"/>
<form-property name="sskills" type="java.lang.String"/>
<form-property name="wskills" type="java.lang.String"/>
<form-property name="hobby" type="java.lang.String"/>
<form-property name="esalary" type="java.lang.String"/>
<form-property name="eloc" type="java.lang.String"/>
<form-property name="ptype" type="java.lang.String"/>
</form-bean>
<form-bean name="enqForm" type="in.ibm.strurts.EnquiryForm"/>
<form-bean name="qualempForm" type="in.ibm.strurts.Qualification_EmployerForm"/>
</form-beans>
<global-exceptions>
<exception key="exception.error" type="java.lang.Exception" path="/logoff.jsp"/>
</global-exceptions>
<global-forwards>
<forward name="enquiry" path="/enquiry.jsp"/>
<forward name="logoff" path="/logoff.jsp"/>
</global-forwards>
<action-mappings>
<action path="/info" name="enqForm" type="in.ibm.strurts.EnquiryAction" validate="true" scope="request" input="/enquiry.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/PersonalDetail.jsp"/>
</action>
<action path="/submit" name="perForm" type="in.ibm.strurts.PersonalAction" validate="true" scope="request" input="/PersonalDetail.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/QualificationDetails.jsp"/>
</action>
<action path="/qualifiction" name="qualempForm" type="in.ibm.strurts.QualificationAction" validate="true" scope="request" input="/QualificationDetails.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/EmployerDetails.jsp"/>
</action>
<action path="/employer" name="qualempForm" type="in.ibm.strurts.EmployerAction" validate="true" scope="request" input="/EmployerDetails.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/Skills.jsp"/>
</action>
<action path="/skills" name="skExpForm" type="in.ibm.strurts.SkillsAction" validate="true" scope="request" input="/Skills.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/expectations.jsp"/>
</action>
<action path="/expectations" name="skExpForm" type="in.ibm.strurts.ExpectationAction" validate="true" scope="request" input="/expectations.jsp">
<exception key="exception.user" type="in.ibm.strurts.IllegalException" path="/logoff.jsp"/>
<forward name="success" path="/logoff.jsp"/>
</action>
</action-mappings>
<message-resources parameter="in.ibm.strurts.messages"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml" property="pathnames"/>
</plug-in>
</struts-config>
9. validations.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation
//DTD Commons Validator Rules Configuration 1.0.1//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0_1.dtd">
<form-validation>
<formset>
<form name="/employer">
<field property="fename" depends="required">
<arg0 key="EMployerName" resource="false"/>
</field>
<field property="sename" depends="required">
<arg0 key="EMployerName" resource="false"/>
</field>
<field property="tename" depends="required">
<arg0 key="EMployerName" resource="false"/>
</field>
</form>
<form name="/skills">
<field property="pskills" depends="required">
<arg0 key="Primary Skill" resource="false"/>
</field>
<field property="sskills" depends="required">
<arg0 key="Secondary Skill" resource="false"/>
</field>
<field property="wskills" depends="required">
<arg0 key="Web Skill" resource="false"/>
</field>
</form>
<form name="/expectations">
<field property="esalary" depends="required">
<arg0 key="Expected Salary Field" resource="false"/>
</field>
<field property="ptype" depends="required">
<arg0 key="Project Type Field" resource="false"/>
</field>
</form>
<form name="/qualifiction">
<field property="sscper" depends="required">
<arg0 key="10th Percentage " resource="false"/>
</field>
<field property="puper" depends="required">
<arg0 key="12th Percentage" resource="false"/>
</field>
<field property="gradper" depends="required">
<arg0 key="Graduation Percentage" resource="false"/>
</field>
</form>
<form name="enqForm">
<field property="name" depends="required,maxlength,minlength">
<arg0 key="lable.user" resource="true"/>
<arg1 key="${var:minlength}" resource="false"/>
<arg2 key="${var:maxlength}" resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>10</var-value>
</var>
</field>
<field property="email" depends="required,email">
<arg0 key="lable.email" resource="true"/>
<arg1 key="lable.email" resource="true"/>
</field>
<field property="phone" depends="long,mask">
<arg0 key="lable.phone" resource="true"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]{10}$</var-value>
</var>
</field>
<field property="sub" depends="lengthRequired">
<arg0 key="lable.subject" resource="true"/>
</field>
<field property="dob" depends="required,date">
<arg0 key="Date" resource="false"/>
<var>
<var-name>datePattern</var-name>
<var-value>dd/MM/yyyy</var-value>
</var>
</field>
</form>
<form name="perForm">
<field property="mname" depends="required">
<arg0 key="lable.personal.mname" resource="true"/>
</field>
<field property="salary" depends="required,float">
<arg0 key="lable.personal.salary" resource="true"/>
</field>
<field property="age" depends="required,integer,intRange">
<arg0 key="lable.personal.age" resource="true"/>
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 name="intRange" key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-value>18</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>65</var-value>
</var>
</field>
<field property="yoe" depends="required,integer,range">
<arg0 key="lable.personal.yoe" resource="true"/>
<arg1 name="range" key="${var:min}" resource="false"/>
<arg2 name="range" key="${var:max}" resource="false"/>
<var>
<var-name>min</var-name>
<var-value>3</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>10</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
10. 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>FormElementWithValFram</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hi</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hi</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
11. validator-rules.xml
------------------------------
Download from net ( GOOGLE-struts1 validation-rules.xml)
JAVA CODE
12. EmployerAction
--------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class EmployerAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Employer Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
13. EnquiryAction
------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class EnquiryAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println(req.getAttribute("enqForm"));
ActionForward af= mapping.findForward("success");
return af;
}
}
14. EnquiryForm
-----------------------
package in.ibm.strurts;
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.validator.ValidatorForm;
public class EnquiryForm extends ValidatorForm {
private String name;
private String batch;
private String gender;
private String city;
private String[] sub;
private String[] top;
private String query;
private String email;
private long phone;
private String dob;
/*@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors err=new ActionErrors();
if(!batch.equals("b33")){
err.add("batch",new ActionError("error.batch.required"));
}
return err;
}*/
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBatch() {
return batch;
}
public void setBatch(String batch) {
this.batch = batch;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String[] getSub() {
return sub;
}
public void setSub(String[] sub) {
this.sub = sub;
}
public String[] getTop() {
return top;
}
public void setTop(String[] top) {
this.top = top;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name="sushil";
this.city="Bangalore";
}
}
15. EnquirySubjectValidation
----------------------------------------
package in.ibm.strurts;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.*;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
public class EnquirySubjectValidation {
public void validateSubject(ActionErrors err,HttpServletRequest req){
System.out.println("in Custom Validation");
String[] sub=req.getParameterValues("sub");
System.out.println(sub.length);
if(sub.length>2){
err.add("sub",new ActionError("error.sub.length"));
}
}
}
16. ExpectationAction
-----------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ExpectationAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Employer Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
17. IllegalException
------------------------
package in.ibm.strurts;
public class IllegalException extends Exception {
}
18. PersonalAction
-------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class PersonalAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println(req.getAttribute("perForm"));
ActionForward af= mapping.findForward("success");
return af;
}
}
19. Qualification_EmployerForm
------------------------------------------
package in.ibm.strurts;
import org.apache.struts.validator.ValidatorActionForm;
public class Qualification_EmployerForm extends ValidatorActionForm {
private String fename;
private String fnoyrs;
private String sename;
private String snoyrs;
private String tename;
private String tnoyrs;
private String sscper;
private String sscyop;
private String ssccol;
private String sscuni;
private String puper;
private String puyop;
private String pucol;
private String puuni;
private String gradper;
private String gradyop;
private String gradcol;
private String graduni;
private String pgper;
private String pgyop;
private String pgcol;
private String pguni;
public String getFename() {
return fename;
}
public void setFename(String fename) {
this.fename = fename;
}
public String getFnoyrs() {
return fnoyrs;
}
public void setFnoyrs(String fnoyrs) {
this.fnoyrs = fnoyrs;
}
public String getSename() {
return sename;
}
public void setSename(String sename) {
this.sename = sename;
}
public String getSnoyrs() {
return snoyrs;
}
public void setSnoyrs(String snoyrs) {
this.snoyrs = snoyrs;
}
public String getTename() {
return tename;
}
public void setTename(String tename) {
this.tename = tename;
}
public String getTnoyrs() {
return tnoyrs;
}
public void setTnoyrs(String tnoyrs) {
this.tnoyrs = tnoyrs;
}
public String getSscper() {
return sscper;
}
public void setSscper(String sscper) {
this.sscper = sscper;
}
public String getSscyop() {
return sscyop;
}
public void setSscyop(String sscyop) {
this.sscyop = sscyop;
}
public String getSsccol() {
return ssccol;
}
public void setSsccol(String ssccol) {
this.ssccol = ssccol;
}
public String getSscuni() {
return sscuni;
}
public void setSscuni(String sscuni) {
this.sscuni = sscuni;
}
public String getPuper() {
return puper;
}
public void setPuper(String puper) {
this.puper = puper;
}
public String getPuyop() {
return puyop;
}
public void setPuyop(String puyop) {
this.puyop = puyop;
}
public String getPucol() {
return pucol;
}
public void setPucol(String pucol) {
this.pucol = pucol;
}
public String getPuuni() {
return puuni;
}
public void setPuuni(String puuni) {
this.puuni = puuni;
}
public String getGradper() {
return gradper;
}
public void setGradper(String gradper) {
this.gradper = gradper;
}
public String getGradyop() {
return gradyop;
}
public void setGradyop(String gradyop) {
this.gradyop = gradyop;
}
public String getGradcol() {
return gradcol;
}
public void setGradcol(String gradcol) {
this.gradcol = gradcol;
}
public String getGraduni() {
return graduni;
}
public void setGraduni(String graduni) {
this.graduni = graduni;
}
public String getPgper() {
return pgper;
}
public void setPgper(String pgper) {
this.pgper = pgper;
}
public String getPgyop() {
return pgyop;
}
public void setPgyop(String pgyop) {
this.pgyop = pgyop;
}
public String getPgcol() {
return pgcol;
}
public void setPgcol(String pgcol) {
this.pgcol = pgcol;
}
public String getPguni() {
return pguni;
}
public void setPguni(String pguni) {
this.pguni = pguni;
}
}
20. QualificationAction
--------------------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class QualificationAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Qualification Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
21. SkillsAction
----------------------
package in.ibm.strurts;
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.ActionForward;
import org.apache.struts.action.ActionMapping;
public class SkillsAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("in Employer Action");
ActionForward af= mapping.findForward("success");
return af;
}
}
PROPERTIES FILE
22. messages.properties
-----------------------------
#lables
lable.user=Username
lable.email=Email
lable.phone=Mobile no
Lable.enquiry.Header=Fill Enquiry Form
lable.subject=Subject
lable.personal.detail=Personal Deatils are required
lable.personal.yoe= Year of Experience
lable.personal.salary=Salary
lable.personal.mname=Mother's Name
lable.personal.age=Age
lable.qualification.detail=<h1>Qualification Details</h1>
lable.qualification.ssc=10th details
lable.qualification.pu=12th details
lable.qualification.grad=Graduation details
lable.qualification.pg=PG details
Lable.expectations.Header=<h1>Your Expectations</h1>
Lable.skill.Header=<h1>Your Skills</h1>
lable.employer.detail=<h1>Previous Employer Details</h1>
lable.employer.first=<h3>First EMployer</h1>
lable.employer.second=<h3>Second EMployer</h1>
lable.employer.third=<h3>Third EMployer</h1>
#errors
errors.header=<font size="4"><UL>
errors.prefix=<LI><span style="color: red">
errors.suffix=</span></LI>
errors.footer=</UL></font>
errors.required= {0} is required.
errors.maxlength={0} length should be max {2} character
errors.minlength={0} length should be min {1} character
errors.email={1} is not in proper format
errors.long={0} should be long
errors.invalid={0} should contains 10 digits
error.batch.required=Batch should be B33
error.sub.length= you should select minimum two {0}
errors.date=date pattern should be dd/MM/yyyy
errors.float={0} should be float
errors.integer={0} should be integer.
errors.range={0} should be between {1}-{2}
exception.user=Exception is thrown
---------------------------------------------------------------------------------
No comments:
Post a Comment