struts2spring

November 6, 2009

struts 1.2 validation framework, tiles framework …

Filed under: Java EE, Struts2 — struts2spring @ 12:08 AM

This is a demo HR application in struts 1.2 to demonstrate validation framework, tiles framework .

HR application start with a Login page where a employee provide their employee id and password to login the application. Login page validate user input if it blank this page does client side and server side validation using Validation framework.

After a successful validation user gets a welcome page. A welcome page demonstrate Tiles Framework.

This is the welcome page of the application .

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>HR</display-name>
 <!-- Action Servlet Configuration -->
 <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>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <!-- Action Servlet Mapping -->
 <servlet-mapping>
 <servlet-name>action</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <!-- The Usual Welcome File List -->
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <taglib>
 <taglib-uri>/WEB-INF/struts-bean</taglib-uri>
 <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/struts-logic</taglib-uri>
 <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/struts-html</taglib-uri>
 <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/sql</taglib-uri>
 <taglib-location>/WEB-INF/sql.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/fmt</taglib-uri>
 <taglib-location>/WEB-INF/fmt.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/c</taglib-uri>
 <taglib-location>/WEB-INF/c.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/struts-nested</taglib-uri>
 <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/struts-tiles</taglib-uri>
 <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>/WEB-INF/x</taglib-uri>
 <taglib-location>/WEB-INF/x.tld</taglib-location>
 </taglib>
 <login-config>
 <auth-method>BASIC</auth-method>
 </login-config>
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
 "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
 <data-sources />
 <form-beans>
 <form-bean name="LoginForm" type="com.struts2spring.LoginForm" />
 </form-beans>
 <global-exceptions />
 <global-forwards>
 <forward name="loginPage" path="/pages/login.jsp" />
 </global-forwards>
 <action-mappings>
 <action input="/pages/login.jsp" name="LoginForm" parameter="method"
 path="/login" scope="request" type="com.struts2spring.LoginAction"
 validate="true">
 <forward name="loginSuccess" path="/pages/loginSuccess.jsp" />
 </action>
 </action-mappings>
 <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"
 bufferSize="4096" debug="0" />
 <message-resources parameter="ApplicationResource" />
 <!--  Tiles plugin    -->
 <plug-in className="org.apache.struts.tiles.TilesPlugin">
 <set-property property="moduleAware" value="true" />
 <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
 <set-property property="definitions-parser-validate"
 value="true" />
 </plug-in>
 <!--  Validator plugin    -->
 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
 <set-property property="pathnames"
 value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
 </plug-in>
</struts-config>

struts-config editor

flow of pages in struts-config .

struts-config editor1

tiles-defs.xml

<?xml version="1.0" encoding="UTF-8" ?>

 <!DOCTYPE tiles-definitions PUBLIC
 "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
 "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">

 <!--
 This is a blank Tiles definition file with a commented example.
 -->

<tiles-definitions>

 <definition name="Tiles.Example" page="/pages/template.jsp">
 <put name="title" type="string" value="Welcome" />
 <put name="header" value="/pages/top.jsp" />
 <put name="menu" value="/pages/left.jsp" />
 <put name="body" value="/pages/content.jsp" />
 <put name="bottom" value="/pages/bottom.jsp" />
 </definition>

 <definition name="${YOUR_DEFINITION_HERE}">
 </definition>

</tiles-definitions>

LoginAction.java

package com.struts2spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class LoginAction extends DispatchAction {

 public ActionForward logintest(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
 throws Exception {
 LoginForm userForm = (LoginForm) form;
 return mapping.findForward("loginSuccess");
 }
}

LoginForm.java

package com.struts2spring;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm {
 private String empid;
 private String password;
 private String method;

 // validate() method in the LoginForm
 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
 // Perform validator framework validations
 ActionErrors errors = new ActionErrors();
 // Only need crossfield validations here
 if (empid == null) {
 errors.add("empid", new ActionError("error."));
 }
 if (password == null) {
 errors.add("password", new ActionError("error.password"));
 }
 return errors;
 }

 public String getEmpid() {
 return empid;
 }

 public void setEmpid(String empid) {
 this.empid = empid;
 }

 public String getPassword() {
 return password;
 }

 public void setPassword(String password) {
 this.password = password;
 }

 public String getMethod() {
 return method;
 }

 public void setMethod(String method) {
 this.method = method;
 }
}

login.jsp

<%@ taglib uri="/WEB-INF/struts-html" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<html:html>

<head>
<title>login</title>
</head>
<body>
<html:form action="/login" method="post"
 onsubmit="return validateLoginForm(this)">
 <html:hidden property="method" />
 <table align="center">

 <tr>
 <td colspan="2">Welcome to the struts2spring login page.</td>
 </tr>
 <tr>
 <td align="right">Employee ID:</td>
 <td><html:text property="empid" /></td>
 </tr>
 <tr>
 <td align="right">Password:</td>
 <td><html:password property="password" /></td>

 </tr>
 <tr>
 <td><html:submit onclick="this.form.method.value='logintest'" /></td>
 <td><html:reset></html:reset></td>
 </tr>
 </table>
 <!-- Begin Validator Javascript Function-->
 <html:javascript formName="LoginForm" />
 <!-- End of Validator Javascript Function-->
</html:form>
</body>
</html:html>

to download code click here svn repository url
https://hrapps.svn.sourceforge.net/svnroot/hrapps

Blog at WordPress.com.