In Struts, you don't have to use the Action class as much as possible because they make your project bloated, you can use the org.apache.struts.Actions.Dispatchaction class to complete the relevant operations you need to do business logic to an action class After inheriting Dispatchaction, you no longer redefine the execute () method, but write your own business method, the execute () method is defined in the Dispatchction abstraction class.
For example, we can inherit Dispatchction to define an AccountAction, centrally manage some operations related to the account, as follows:
package onlyfun.caterpillar; import javax.servlet.http *;. import org.apache.struts.action *;. import org.apache.struts.actions *;. public class AccountAction extends DispatchAction {public ActionForward login (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {// login operation related ......} public ActionForward logout (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {// operations related to logout. .....} Public ActionForwa RD method1 (ActionMapping Mapping, ActionMApping Mapping, HttpservletRequest Request, HttpservletResponse Response) Throws Exception {//Method1 related operations .....}
We no longer redefine the execute () method, but define our own login (), logout () and other methods, which receive the same parameters as Execute (), and also pass back the ACTIONFORWARD object. When using Dispatchaction, we have to define in struts-config.xml: pat = "/ account"
TYPE = "Onlyfun.caterpillar.accountAction"
Parameter = "Method"
Name = "Userform">
Name = "greeting"
Path = "/ login / greeting.jsp" />
Mainly on the Prameter's properties, we specify the method we want to use with the Method request parameter, such as the following URL will execute the login () method: http: // localhost: 8080 / hellostruts / account.do? Method = login & name = catillar & password = 1234 Note In the request parameters, we include Method = login to specify the login () method, the same, if you want to perform the logout () method, as follows: http: // localhost: 8080 / Hellostruts / Account.do? Method = Logout