Struts provides a special input field called html:cancel.You can use this input field to cancel a form submission. when you use the isCancelled method in the action handler to see whether the action was cancelled.
Syntax of isCancelled method :-
protected boolean isCancelled(HttpServletRequest request)
This method return the boolean value. if the current form's cancel button was pressed then return true otherwise return false.
Create the input form :-
index.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>
Create Actionhandler class :-
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);
}
}
Output
