LookupDispatchAction

LookupDispatchAction

Previous Home Next

 

LookupDispatchAction class is a subclass of  DispatchAction  It does a reverse lookup on the resource bundle. You have to implement a special method that map to the key and then gets the method whose name is associated with the key into the Resource Bundle.

 
The comman use of LookupDispatchAction class :-

This class is useful if the method name in the Action is not driven by its name in the front end but by the Locale independent key into the resource bundle. Since the key is always the same.
In the LookupDispatchAction is based on a lookup of a key value but DispatchAction is specifying the method name directly.

Create an example of LookupDispatchAction class :-

Directory Structure of LookupDispatchActionExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title><bean:message key="label.title"/></title>       
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<html:form action="/lookupAction" >
<table cellpadding="3" cellspacing="3">
<tr><td><bean:message key="label.name"/></td><td><html:text name="lookupForm" property="name" /></td></tr>
<tr><td><bean:message key="label.address"/></td><td><html:text name="lookupForm" property="address" /></td></tr>
<tr><td><bean:message key="label.city"/></td><td><html:text name="lookupForm" property="city" /></td></tr>
<tr><td><bean:message key="label.state"/></td><td><html:text name="lookupForm" property="state" /></td></tr>
<tr><td><bean:message key="label.country"/></td><td><html:text name="lookupForm" property="country" /></td></tr>
<tr><td><html:submit property="method"><bean:message key="label.student"/></html:submit></td>
<td><html:submit property="method"><bean:message key="label.employ"/></html:submit></td></tr>
</table>
</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="lookupForm" type="org.r4r.struts.LookupForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  <action path="/dispatcher" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
  <action path="/lookupAction" name="lookupForm" type="org.r4r.struts.LookupAction" parameter="method" input="/index.jsp">
  <forward name="student" path="/student.jsp"/>
  <forward name="employ" path="/employ.jsp"/>
  </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

LookupForm.java

package org.r4r.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class LookupForm extends ActionForm {
	private String name;
	private String address;
	private String city;
	private String state;
	private String country;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
}

LookupAction.java

package org.r4r.struts;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;

public class LookupAction extends LookupDispatchAction {
	@SuppressWarnings("unchecked")
	@Override
	protected Map getKeyMethodMap() {
		Map map=new HashMap();
		map.put("label.student", "student");
		map.put("label.employ", "employ");
		return map;
	}
	public ActionForward student(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("student");
    }
	public ActionForward employ(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("employ");
    }
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
# Project Struts1.3_LookupDispatchActionExample
label.title=Struts 1.3 LookupDispatchAction Example
label.header=Struts 1.3 LookupDispatchAction Example
label.header.student=This is Student Page Output
label.header.employ=This is Employ Page Output
label.name=Name
label.address=Address
label.city=City
label.state=State
label.country=Country
label.student=Student
label.employ=Employ

employ.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title><bean:message key="label.title"/></title>       
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<h3 style="color: red;"><bean:message key="label.header.employ"/></h3>
<table cellpadding="3" cellspacing="3" border="1">
<tr><td><bean:message key="label.name"/>:-</td><td><bean:write name="lookupForm" property="name"/></td></tr>
<tr><td><bean:message key="label.address"/>:-</td><td><bean:write name="lookupForm" property="address"/></td></tr>
<tr><td><bean:message key="label.city"/>:-</td><td><bean:write name="lookupForm" property="city"/></td></tr>
<tr><td><bean:message key="label.state"/>:-</td><td><bean:write name="lookupForm" property="state"/></td></tr>
<tr><td><bean:message key="label.country"/>:-</td><td><bean:write name="lookupForm" property="country"/></td></tr>
</table>
</body>
</html>

student.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title><bean:message key="label.title"/></title>       
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<h4 style="color: red;"><bean:message key="label.header.student"/></h4>
<table cellpadding="3" cellspacing="3" border="1">
<tr><td><bean:message key="label.name"/>:-</td><td><bean:write name="lookupForm" property="name"/></td></tr>
<tr><td><bean:message key="label.address"/>:-</td><td><bean:write name="lookupForm" property="address"/></td></tr>
<tr><td><bean:message key="label.city"/>:-</td><td><bean:write name="lookupForm" property="city"/></td></tr>
<tr><td><bean:message key="label.state"/>:-</td><td><bean:write name="lookupForm" property="state"/></td></tr>
<tr><td><bean:message key="label.country"/>:-</td><td><bean:write name="lookupForm" property="country"/></td></tr>
</table>
</body>
</html>

Output




Previous Home Next