< html:password /> html tag

< html:password /> html tag

Previous Home Next

 

This tag is renders an HTML <input> element of type password <html:password/>.This tag is populated from the specified value or the specified property of the bean.

Note:- This tag must be nested inside the body of a form tag
Example:-

<%@ taglib  uri="http://struts.apache.org/tags-html" prefix="html"%>

<html:form action="/Login" >
user name : <html:text name="LoginForm" property="userName" />
password :<html:password name="LoginForm" property="password" />
<html:submit value="Login"/>
</html:form>

The struts <password> tag have following attributes:-
  • property * :- This is identifies the name of the input field being processed. 
  • accesskey :-This attributes is used to immediately move focus to this element.
  • style :- In this element you can applied CSS style.
  • styleClass :- In this element you can applied CSS stylesheet class. 
  • styleId :- This is Identifier to be assigned to this HTML element and renders an 'id' attribute.
  • tabindex :- This is tab ascending positive integers order for this element.
  • title* :- This attributes is the consultant title for this element.
  • titleKey :-This attributes is the consultant title for this element basically this is message resources key.
  • value :-This specifies the value of bean of input element ,if the ActionForm bean associated with the parent form tag has a property that is matches the value attributes..

 
The Following are many other attributes which we can use :
  • alt
  • altKey
  • disabled
  • indexed
  • name
  • onblur
  • onchange
  • onclick
  • ondblclick
  • onfocus
  • onkeydown
  • onkeypress
  • onkeyup
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup
  • disabled
  • maxlength
  • size
  • readonly
  • redisplay

Directory Structure of PasswordTagExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Struts html:password tag example</title>
</head>
<body>
<h3>Struts html:password tag example</h3>
<html:form action="/passwordAction">
<bean:message key="label.name"/>:
<html:text property="name" name="passwordForm"/><br/><br/>
<bean:message key="label.password"/>:
<html:password property="password" name="passwordForm"/><br/>
<br/>
<html:submit><bean:message key="label.submit" /></html:submit>
<html:reset><bean:message key="label.reset" /></html:reset>
</html:form>
</body>
</html>

web.xml

<!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>Maven Struts Examples</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> 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>
    <form-bean name="passwordForm" type="org.r4r.struts.PasswordForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action path="/password" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
		<action path="/passwordAction" type="org.r4r.struts.PasswordAction" name="passwordForm" input="/index.jsp">	
         <forward name="success" path="/success.jsp"/>
		</action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

PasswordTagForm.java

package org.r4r.struts;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.CommonsMultipartRequestHandler;
import org.apache.struts.upload.MultipartRequestWrapper;

@SuppressWarnings("serial")
public class PasswordForm extends ActionForm {
	private String name;
	private 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;
	}

	@SuppressWarnings("unchecked")
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// reset properties
		if (request instanceof MultipartRequestWrapper) {
            MultipartRequestWrapper wrapper = (MultipartRequestWrapper) request;  
            CommonsMultipartRequestHandler handler = new CommonsMultipartRequestHandler();  
            handler.setMapping(mapping);  
            try {  
                handler.handleRequest(request);  
            } catch (ServletException ex) {  
               System.out.println(ex);  
            }  
            Map paramMap = handler.getTextElements();  
            for (Iterator it = paramMap.keySet().iterator(); it.hasNext() ;)  {  
                String key = (String) it.next();  
                String value = (String) paramMap.get(key);  
                wrapper.setParameter(key, value);  
            }  
        }  
	}

}

PasswordTagAction.java

package org.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 PasswordAction extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) throws Exception{
		@SuppressWarnings("unused")
		PasswordForm passwordForm=(PasswordForm)form;
		return mapping.findForward("success");
	}
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
# Project Struts1.3_PasswordTagExample
#label message
label.name = User Name 
label.password = User Password 
label.submit = Submit
label.reset = Reset

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Struts html:password tag example</title>
</head>
<body>
<h3>Struts html:password tag Example</h3>
<h4>Welcome,<bean:write name="passwordForm" property="name" /></h4>
<h4>Your password is,<bean:write name="passwordForm" property="password" /></h4>
</body>
</html>

Output




Previous Home Next