< html:cancel /> html tag

< html:cancel /> html tag

Previous Home Next

 

This is nested tag of <html:form> .It is render of cancel button. The struts cancel <html:cancel> tag is renders an html<input> tag of submit type. When we press on the button the ActionServlet bypass the validate() method .

</html:cancel></html:form>

 
<html:cancel> must be nested in <html:form>.</html:form></html:cancel>
<html:cancel><html:form>
</html:form></html:cancel>
<html:cancel><html:form>

<html:form  action="Loginaction" >
<html:text property="User name"/>
<html:password property="password"/>
<html:submit/>
<html:cancel/></html:form>

Following are more attributes:

1.accesskey :-This attributes is used to move focus immediately to this element

2.onchange :- This element loses input focus and its value has changed than  event handler executed

3.property :-This is used to name of the request parameter that will be included with this submission, set to the specified value. you can set this attribute to a value other than the default bacause this will not be recognized as the cancel key. 

The Following are many other attributes which we can use :

  • alt 
  • altKey 
  • bundle 
  • dir 
  • disabled 
  • lang 
  • onblur   
  • onclick 
  • ondblclick 
  • onfocus
  • onkeydown 
  • onkeypress 
  • onkeyup 
  • onmousedown 
  • onmousemove 
  • onmouseout 
  • onmouseover 
  • onmouseup  
  • style 
  • styleClass 
  • styleId 
  • tabindex 
  • title 
  • titleKey 
  • value
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