package net.viralpatel.struts.helloworld.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
public class UserManagementAction extends DispatchAction {
public ActionForward create(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("message", "User created successfully");
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("message", "User deleted successfully");
return mapping.findForward("success");
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("message", "User updated successfully");
return mapping.findForward("success");
}
public ActionForward block(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("message", "User blocked successfully");
return mapping.findForward("success");
}
}
Code language: Java (java)
In above code, we have created separate methods (create(), delete(), update() and block()) for each functionality. Also note that the method signature of these methods are exactly similar to the execute() method of Action class file. public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { }
Code language: Java (java)
<action path="/user" parameter="parameter"
type="net.viralpatel.struts.helloworld.action.UserManagementAction">
<forward name="success" path="/UserSuccess.jsp" />
<forward name="failure" path="/UserSuccess.jsp" />
</action>
Code language: HTML, XML (xml)
We have added an extra attribute in <action> tag, parameter=”parameter”. DispatchAction will read a request parameter called “parameter” and its value will decide the method to be called. Suppose you have a request parameter “parameter” with value “create”, Dispatch Action will call create() method from your Action file. <%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>Dispatch Action Example - viralpatel.net</title>
</head>
<body>
<h2>User Management (Dispatch Action Example)</h2>
<html:link href="user.do?parameter=create">Create User</html:link>
|
<html:link href="user.do?parameter=delete">Delete User</html:link>
|
<html:link href="user.do?parameter=update">Update User</html:link>
|
<html:link href="user.do?parameter=block">Block User</html:link>
</body>
</html>
Code language: HTML, XML (xml)
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>Dispatch Action Example - viralpatel.net</title>
</head>
<body>
<h3>User Message (Dispatch Action Example)</h3>
<center>
<h3><%= request.getAttribute("message") %></h3>
<center>
</body>
</html>
Code language: HTML, XML (xml)
In UserManagement.jsp, we have created links using <html:link> tag and passed a parameter “parameter” with values create, delete, block etc. This value is fetched by the Dispatch Action and is used to call corresponding method in Action class. Thus if we click Update User link, a parameter update will be passed to the action class and corresponding update() method will be called by framework. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Excelente manual!!! Muchas gracias
Thank's
@Carlos: Thanks for the good words :) Do read other articles about Struts and also feel free to subscribe for the newsletter.
Nice Tutorial,Very Good
If we have a from in which we are trying to use Dispatch action, What is the best way of implementing it. I have used the parameter in action tag of a Form, something like, action='dynaAction.do?param=ParamValue'. depends on the action selected, we can change the URL for the form. Do you have any other approach in doing this.
Thanks,
Kumar
hello viral
my question is that ,cannot we use buttons instead of html-link ??.....
b'coz when I m using buttons instead of links I m getting following error
" Request[/ClientForm] does not contain handler parameter named 'parameter'. This may be caused by whitespace in the label text"....
Hi Neelam,
The possible reason of error in case of using button instead of link is that it is not able to get parameter to decided which method to trigger in Struts. Create a field (hidden or select box) in your form with name="parameter" and set its value to method name that you want to call.
Hope this will solve the issue.
hi viral
I have tried this using
**********jsp...************
***********struts-config...*************
********ApplicatonResources...***********
label.save=save
*********************ClientAction_Buttons.java***************************
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("label.add", "add");
map.put("label.edit", "edit");
map.put("label.search", "search");
map.put("label.save", "save");
return map;
}
public ActionForward add(
ActionMapping mapping,
ClientForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("You are in add function.");
return mapping.findForward("add");
}
**************
but now I m getting the following error
javax.servlet.ServletException: DispatchMapping[/ClientForm] does not define a handler property..
can you please tell me what I doing wrong here???
Regards
Neelam
"javax.servlet.ServletException: DispatchMapping[/ClientForm] does not define a handler property..
can you please tell me what I doing wrong here???"
Your add() method is having a parameter of type ClientForm, Replace the ClientForm with ActionForm and check if it works.
I think this is not a good solution. In some situations it is not useful and cannot be implemented. How can we extend both ActionSupport and DispatchAction. In modern OOP, a class should be have one and only one superclass.
Hi viral,i am daily visitor of u r website,u r posts are very good and i am following the same.Me working as a s/w professional in a small organization.could you pls post one sample project with dao,vo,action,domain layer.
Hi Neelam,
I guess, now you have used a hidden field named "parameter" on the jsp, but you have not
created a property by the name "parameter" on the corresponding form for that jsp.
if we use dispatch action how many formbean(Action Forms) classes we have to create.
if i want to put a specific method of dispatch action in thread, then what will i have to do .