Basic knowledge point of struts1-DispachAction

Source: Internet
Author: User

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

 

 

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.