Struts DispatchAction Example

The DispatchAction class (org.apache.struts.actions.DispatchAction) provides a way to group all related functions into a single action class. It’s a useful mechanism to avoid create separate action classe for each function.

Download this Struts DispatchAction example – Struts-DispatchAction-Example.zip

To implement this mechanism, your action class need to extends org.apache.struts.actions.DispatchAction class, this action class does not need to implement the execute() method as normal action class does. Instead, the DispatchAction class will execute the method base on the incoming request parameter – method. For example, if the parameter is “method=chinese”, then the chinese() method will be execute.

Example

A action class extends the DispatchAction, and contains four methods to set the locale into the Struts session attribute for the localization.


public class LanguageSelectAction extends DispatchAction{
	
	public ActionForward chinese(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
	throws Exception {
		
		request.getSession().setAttribute(
				Globals.LOCALE_KEY, Locale.SIMPLIFIED_CHINESE);
		
		return mapping.findForward("success");
	}
	
	public ActionForward english(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
	throws Exception {
		
		request.getSession().setAttribute(
				Globals.LOCALE_KEY, Locale.ENGLISH);
		
		return mapping.findForward("success");
	}
	
	public ActionForward german(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
	throws Exception {
		
		request.getSession().setAttribute(
				Globals.LOCALE_KEY, Locale.GERMAN);
		
		return mapping.findForward("success");
	}
	
	public ActionForward france(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
	throws Exception {
		
		request.getSession().setAttribute(
				Globals.LOCALE_KEY, Locale.FRANCE);
		
		return mapping.findForward("success");
	}
	
}

This Struts html tag will execute the chinese() method.

Chinese

This Struts html tag will execute the english() method.

English

This Struts html tag will execute the german() method.

German

This Struts html tag will execute the france() method.

France

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
10 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
10 Comment authors
kamalsaurav dasguestmocineKailash Recent comment authors
newest oldest most voted
kamal
Guest
kamal

org.apache.jasper.JasperException: An exception occurred processing JSP page /pages/multi-language.jsp at line 33

30: German
31: France
32:
33:
34:
35: :
36:

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.struts.chain.commands.servlet.PerformForward.handleAsForward(PerformForward.java:113)
org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:96)
org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Exception creating bean of class com.mkyong.common.form.UserForm under form name userForm
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
org.apache.jsp.pages.multi_002dlanguage_jsp._jspService(multi_002dlanguage_jsp.java:110)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.struts.chain.commands.servlet.PerformForward.handleAsForward(PerformForward.java:113)
org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:96)
org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause

guest
Guest
guest

some classes extend ActionSupport while some extend DispatchAction. how are these two different

mocine
Guest
mocine

Thanks very much

Kailash
Guest
Kailash

Respected MkYong,

Thanks a lot for posting simple example which is very useful to understand.

However i have one query. Can you help me?

Can we hide URL? i mean Can we hide method name? User should not aware which method we are calling..

Thanks in Advance.

Kailash

Dharmendra Sahu
Guest
Dharmendra Sahu

Thanks for your great tutorial.
I want to know one thing
How DispacherAction internally call execute() method.

Thanks..

Shrikant Kale
Guest
Shrikant Kale

I have done the same prog. with buttons and using the scope but problem is that its not showing any result —————————————————————————————————————- <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html:form action="cust"> Student Id : <html:text property="sid"/> <br> Student Name : <html:text property="name"/> <br><br> <html:submit property="method" value="saveCustomer"/> <html:submit property="method" value="deleteCustomer"/> </html:form> ————————————————————————————————————————- <struts-config> <form-beans> <form-bean name="CustomerDispatchForm" type="com.myapp.struts.CustomerDispatchForm"/> </form-beans> <action-mappings> <action name="CustomerDispatchForm" path="/cust" scope="session" type="com.myapp.struts.CustomerDispatchAction" parameter="method"> <forward name="success" path="/Success.jsp"/> </action> </action-mappings> </struts-config> ———————————————————————————————————————– public class CustomerDispatchForm extends org.apache.struts.action.ActionForm { protected String sid; protected String name; public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public… Read more »

v1per
Guest
v1per

Hello,

Really nice series, thanks.

By the way, I couldn’t find information regarding struts-extras dependencies.

saurav das
Guest
saurav das

yes , it should be mentioned very well in this tutorial,else it becomes very difficult to understand then

aditya san
Guest
aditya san

hi, how can i handle the case where i am sending a new language X. i would like to send a specific error page should the language be x. thanks

aaaaa
Guest
aaaaa

Thanks very much. I learned dispatch actions with your this writing.

trackback
Struts Tutorials | Tutorials

[…] DispatchAction Example Allow you to group all related functions into a single action class. […]