DispachAction is a dynamic specified Action. That is to say, different request and url parameters at the view layer are intercepted by ActionServlet,
And in the struts-config.xml according to the request to get different parameters to specify (CALL) Action in different methods, according to the return value of the method to jump to the corresponding page.
This can effectively solve the problem of Action expansion. In the past, we inherited the Action in struts to process only one type of request. That is to say, different requests have corresponding Action classes. In this way, the Action classes will become more and expand as actions. DispachAction can be used to specify the method to be called based on different requests. This effectively solves the problem of Action expansion.
How to Use DispachAction:
1. Write a UserAction to inherit DispachAction
Write an addUser method to add a user
[Java]
// Add a User
Public ActionForward addUser (ActionMapping mapping, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
New UserService (). saveUser (new User ());
Return mapping. findForward ("OK ");
}
// Add a User
Public ActionForward addUser (ActionMapping mapping, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
New UserService (). saveUser (new User ());
Return mapping. findForward ("OK ");
}
Write a deleteUser method to delete a user.
[Java]
// Delete a User
Public ActionForward deleteUser (ActionMapping mapping, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
Return mapping. findForward ("delok ");
}
// Delete a User
Public ActionForward deleteUser (ActionMapping mapping, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
Return mapping. findForward ("delok ");
}
2. on the page
[Html]
<SPAN style = "FONT-SIZE: 18px"> <form action = "User. do? Command = addUser "method =" post ">
Username: <input type = "text" name = "username"> <br>
Password: <input type = "password" name = "password">
<Input type = "submit" value = "submit">
</Form>
<A href = "User. do? Id = 1 & command = deleteUser "> delete a user whose id is 1 </a> </SPAN>
<Form action = "User. do? Command = addUser "method =" post ">
Username: <input type = "text" name = "username"> <br>
Password: <input type = "password" name = "password">
<Input type = "submit" value = "submit">
</Form>
<A href = "User. do? Id = 1 & command = deleteUser "> delete a user whose id is 1 </a>
3. Configure in struts-config.xml
[Html]
Action-mappings>
<Action path = "/User"
Parameter = "command"
Type = "com. jxau. action. UserAction">
<Forward name = "OK" path = "/add_ OK .jsp"> </forward>
<Forward name = "delok" path = "/del_ OK .jsp"> </forward>
</Action>
</Action-mappings>
<Action-mappings>
<Action path = "/User"
Parameter = "command"
Type = "com. jxau. action. UserAction">
<Forward name = "OK" path = "/add_ OK .jsp"> </forward>
<Forward name = "delok" path = "/del_ OK .jsp"> </forward>
</Action>
</Action-mappings>
Conclusion: DispatchAction can be used to dynamically control the Action and manage a business logic based on the principle of one template and one Action. Effectively prevents Action Expansion