Saturday, January 29, 2011

Dispatch Action Example in Struts

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>Smaple</display-name>
<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>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<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>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

Struts-config.xml :


<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<data-sources />
<form-beans>
<form-bean name="LoginForm" type="com.test.LoginFormBean" />
</form-beans>

<global-exceptions />

<global-forwards>
<forward name="error" path="/error.jsp" />
</global-forwards>

<action-mappings>
<action forward="/login.jsp" path="/home" type="org.apache.struts.ActionForward" />
<action input="/login.jsp" name="LoginForm" path="/login" type="com.test.LoginAction" validate="true" parameter="method">
<forward name="success" path="/layout.jsp" />
<forward name="failure" path="/failure.jsp" />
</action>

</action-mappings>
<controller />
<message-resources parameter="com/test/ApplicationResource" />
<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>


LoginAction.java :

package com.test;

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 loginMethod(ActionMapping maping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

String returnParam = null;

LoginFormBean loginFormBean = (LoginFormBean)form;

String username = loginFormBean.getUsername();

String password = loginFormBean.getPassword();

System.out.println("Usename : " +username+ " Password : "+password);

if(username.equals("admin") && password.equals("admin")){

returnParam = "success";

}else{

returnParam = "failure";

}

return maping.findForward(returnParam);

}

}

Validation.xml :

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE form-validation PUBLIC

"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"

"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">

<form-validation>


<global>

<!-- An example global constant

<constant>

<constant-name>postalCode</constant-name>

<constant-value>^\d{5}\d*$</constant-value>

</constant>

end example-->

</global>

<formset>

<form name="LoginForm">

<field

property="username"

depends="required">

<arg0 key="error.userName.required"/>

</field>

<field

property="password"

depends="required,mask">

<arg0 key="error.password.required"/>

<var>

<var-name>mask</var-name>

<var-value>^[0-9a-zA-Z]*$</var-value>

</var>

</field>

</form>

</formset>

</form-validation>

LoginFormBean.java :


package com.test;

import org.apache.struts.validator.ValidatorForm;

public class LoginFormBean extends ValidatorForm {

/**

*

*/

private static final long serialVersionUID = 1L;

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}
}

index.jsp :

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<jsp:forward page="home.do"></jsp:forward>

login.jsp :

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>

<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>

<html:form action="login.do?method=loginMethod">
<html:errors />
<html:javascript formName="LoginForm"/>
User Name :<html:text property="username"></html:text> <br />
Password : <html:password property="password"></html:password> <br />
<html:submit value="Login"></html:submit> <html:cancel value="Cancel"></html:cancel>

</html:form>



layout.jsp :


<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>

You are in Layout page


failure.jsp :


<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>

<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>

<html:html>

<head>

<title></title>

</head>

<body>

You are in Failure Page

</body>

</html:html>

No comments: