Canceling the JavaScript Validation in Validator Framework
Previous | Home | Next |
Add a 'Cancel' button to loginForm can cause problems if not handled correctly.User want to cancel the operation altogether,usually with a cancel button.
When cancel button submits the form-so the java script tries validate it.The server side pieces can be made to acknowledge the cancel button,but java script is client side and unaware of the framework.So,Cancel a form ,a user must first appease the java script validation,it is not good.so solve this problem ,struts provides a bCancel javaScript variable.if bCancel is set to true, the java script validation will also return true,it means the cancel request to pass through to the container.<html:cancel onclick="bCancel=true;"> <bean:message key="button.cancel"/> </html:cancel>- When the Cancel button is clicked, The request parameter org.apache.struts.action.Action.isCancelled method is send.if(this.isCancelled(request)){ System.out.println("This has been cancelled::"); return mapping.findForward("home"); }
Directory Structure of Login Example in Struts 1.3 Using MyEclipse IDEindex.jsp<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%><html:html> <head> <title>Struts First Program,Struts First Application</title> </head> <body bgcolor="white"> <h1>R4R Struts Tutorials</h1> <html:link href="R4RLoginForm.jsp">Login</html:link> </body> </html:html>web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.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>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</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> </web-app>struts-config.xml<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <!-- Form Beans Configuration --> <form-beans> <form-bean name="loginForm" type="com.r4r.struts.R4RLoginActionForm" /> </form-beans> <!-- Action Mappings Configuration --> <action-mappings> <action path="/login" type="com.r4r.struts.R4RLoginAction" name="loginForm" scope="request" cancellable="true" input="/R4RLoginForm.jsp"> <forward name="failed" path="/failed.jsp" /> <forward name="success" path="/user.jsp" /> <forward name="admin" path="/admin.jsp" /> <forward name="cancel" path="/cancel.jsp" /> </action> </action-mappings> </struts-config>
R4RLoginForm.jsp<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%><%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>Login Form</title> </head> <body> <center> <h2>Login Form</h2> <font size="2"> (Very First Examples Of Struts Input View)</font> <table> <html:form action="/login"> <tr> <td>User Name</td> <td><html:text property="name" /></td> </tr> <tr> <td>Password</td> <td><html:password property="password" /></td> </tr> <tr> <td colspan="2" align="center"><html:submit /> <html:reset /><html:cancel/> </td> </html:form> </table> </center> </body> </html>R4RLoginAction.javapackage com.r4r.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.ActionForward; import org.apache.struts.action.ActionMapping; public class R4RLoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String key = "failed"; if (isCancelled(request)){ return mapping.findForward("cancel"); } R4RLoginActionForm actionForm = (R4RLoginActionForm) form; if (actionForm.getName().equals("admin")&& actionForm.getPassword().equals("123")) { key = "admin"; } if (actionForm.getName().equals("xyz")&& actionForm.getPassword().equals("145")) { key = "success"; } return mapping.findForward(key); } }R4RLoginActionForm.javapackage com.r4r.struts;import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class R4RLoginActionForm extends ActionForm{ private static final long serialVersionUID = 1L; String name; String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public void reset(ActionMapping mapping, HttpServletRequest request) { name = null; password = null; } }cancel.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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>Cancel Button in Struts Web based Application</title> </head> <body> <center> <h2>Cancel Button in Struts Web based Application</h2> </center> Cancelled Successfully!! </body> </html>admin.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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>Admin Control Panel</title> </head> <body> <center> <h3>Admin Control Panel</h3> <font size="1">Welcome you ..................</font></center> </body> </html>user.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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>User Control Paneltitle</title> </head> <body> <center> <h3>User Control Panel</h3> <font size="1">Welcome you ..................</font></center> </body> </html>failed.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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>Login Failed</title> </head> <body> <center> <h3>Login Failed</h3> <font size="1">User Name and Password May Be Wrong Try again.</font> </center> </body> </html>Output
Previous | Home | Next |