Working with Struts Custom Tags

Working with Struts Custom Tags

Previous Home Next

 

The strusts 'custom' tag is something you write so that the compiler/interpreter understands what it is supposed to do.The struts framework support custom tag because HTML Tags are pre-defined. You cannot write HTML tags with names you like.

 The custom tag has following parts when We implements into struts framework:-

1.
 javax.servlet.jsp.tagext.Tag or javax.servlet.jsp.tagext.BodyTag are interface which is implementing one of two interface in Java class. Tag interface is implemented only for bodyless tags Methods to implement include doStartTag(), doEndTag() for the Tag interface, but also doInitBody() and doAfterBody().

2.Write a tag library descriptor, which is a XML document containing information about all the custom tags. This file save the .tld extension.

3.Declare the tag library descriptor in the web.xml file. web.xml is the standard descriptor file for any web application.

Directory Structure of CustamTagExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html>
<head>
<title><bean:message key="label.title"/></title>
</head>
<body>
<div style="color: red;">
<html:errors/>
</div>
<h3><bean:message key="label.header"/></h3>
<html:form action="/customAction">
<bean:message key="label.name"/>:
<html:text property="name" name="customForm"/><br/>
<bean:message key="label.password"/>:
<html:text property="password" name="customForm"/><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="customForm" type="org.r4r.struts.CustomForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="customForm"
      input="/index.jsp"
      name="customForm"
      path="/customAction"
      scope="request"
      type="org.r4r.struts.CustomAction"
      cancellable="true">
      <forward name="success" path="/success.jsp" />
    </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

CustomForm.java

package org.r4r.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

@SuppressWarnings("serial")
public class CustomForm 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;
	}
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if (name == null || name.length() < 1) {
		errors.add("name", new ActionMessage("error.name.required"));
		}
		if (password == null || password.length() < 1) {
		errors.add("password", new ActionMessage("error.password.required"));
		}
		return errors;
		}
}

CustomAction.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 CustomAction extends Action {
public ActionForward execute ( ActionMapping mapping  ,   ActionForm form  ,  
	HttpServletRequest request  ,   HttpServletResponse response  )  { 
@SuppressWarnings("unused")
CustomForm customForm = (CustomForm) form;
return mapping.findForward("success");
}
}

ApplicationResources.properties

#label message
label.title=Struts Custom Tag Example
label.header=Struts Custom Tag Example
label.name = User Name 
label.password = Password
label.submit = Submit
label.reset = Reset
error.name.required= Name is required<br/>
error.password.required= Password is required

success.jsp

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html>
<head>
<title><bean:message key="label.title"/></title>
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<h4>Welcome , <bean:write name="customForm" property="name"/></h4>
</body>
</html>

Output




Previous Home Next