Cancel Button in Struts Web based Application

Cancel Button in Struts Web based Application

Previous Home Next

 

In this section we are going to make a cancel button.We can add the cancel button in login.jsp .When user click on cancel button than Action class execute method will call. Here We will check if cancel is true then forwards to next view (cancel.jsp). struts-config.xml is called a new cancel.jsp page.


 
When we clicked on button (submit/cancel) then the execute() method of Action class is called every time.To handle cancel button we have add following code within execute( ) method. 
We must have set cancellable="true" to enable cancel button handling. If we miss it then exception will thrown. When we click on cancel button then Action class will retrun a key we have forward to next view. Configure forward like View we can put cancel button using following tag You can download full example from
http://r4r.co.in/java/struts/basic/tutorial/r4rcancelbutton.zip 

Directory Structure of Login Example in Struts 1.3 Using MyEclipse IDE



index.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.java

package 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.java

package 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