Deep parsing of the controller Dispatchaction_java in the struts framework of Java

Source: Internet
Author: User

The form processor in struts is actionform, and the controllers in struts are mainly action, and dispatchaction controllers.
Action
in struts, all users are actionservlet, and the actual work is handled by the Action object, Actionservlet can create actionmapping objects from the configuration file. Find the appropriate action from the Actionmapping object, and then transfer the user request to the action.
for struts a actionmapping can only generate a single Action object, and when a user initiates a request, it checks if the desired action object exists and, if not, generates an action object that will be used later in the process.
When we use the action we need to inherit the class of arg.apache.struts.action.Action, add the required business logic to the subclass, and these subclasses will return the Actionforward object, Actionservlet accept the object, and the page Forwards to the specified page, which sends the result of the user request to the corresponding page. We are configuring in Struts-config.xml. The main properties of the configuration are as follows:
(1)   Path property: Access the URL address of the action, and when the user requests the path and URL match, Actionservlet sends the user request to action.
(2)   Type property: Specifies the class that handles the request's action, and needs to write the package path of the class file.
(3)   Name property: Specifies the Actionform name that our action uses, which must be defined in <form-beans>.
(4)   Scope property: Specifies the scope of use of the Actionform, the default is the session range.
(5)   Input property: Specifies that the form validation error is turned to the page.
(6)   Validate property: Indicates whether the Validate method in Actionform is automatically invoked to validate the form. The
Configuration example shows the following code.

 <struts-config> <form-beans> <form-bean name= "LoginForm" type= "Com.bjpowernode.struts.LoginActionForm"/> </form-beans> <action-mappings> <action p Ath= "/login" type= "com.bjpowernode.struts.LoginAction" name= "LoginForm" scope= "R" Equest "> <forward name=" Success "path="/login_success.jsp "/> <forwa RD name= "error" path= "/login_error.jsp"/> </action> </action-mappings> </STRUTS-CONFIG&G 
T 


Problem
when we completed the user to increase the search operation using the Struts framework, we need to change the search for additions and deletions to establish four different action, if there are more additions and deletions to check operations, such as the material additions and deletions to check also need to establish four action, which caused a lot of action.
Solution to the problem
In the action class in struts, only one execute () method is provided, and a user request URL can only correspond to a servlet. Another controller class Org.apache.struts.actions.DispatchAction is provided in struts, a class that can be completed with several methods required by the relevant business logic in a Dispatchaction class, we inherit the Dispatchaction class without heavy Write the Execute () method, but write your own method to handle different actions in different ways. Delete User add-in check the corresponding action, establish useraction.

The calling code in the interface is shown below.

<body> 
<a href= "user/user_maint.do?command=list" title= "please click to access User Management System" > User Management system </a> 
</ Body> 


Where the list corresponds to the list method in Useraction, the string passed is the same as the method name in Useraction.
The code in Useraction looks like this:

Packagecom.bjpowernode.drp.web.actions; 
Importjava.util.Date; 
  
Importjava.util.List; 
Importjavax.servlet.http.HttpServletRequest; 
  
Importjavax.servlet.http.HttpServletResponse; 
Importorg.apache.commons.beanutils.BeanUtils; 
Importorg.apache.struts.action.ActionForm; 
Importorg.apache.struts.action.ActionForward; 
importorg.apache.struts.action.ActionMapping; 
  
Importorg.apache.struts.actions.DispatchAction; 
Importcom.bjpowernode.drp.manager.UserManager; 
Importcom.bjpowernode.drp.model.User; 
  
Importcom.bjpowernode.drp.web.forms.UserActionForm; Public Classuseraction extends Dispatchaction {protected Actionforward list (actionmapping mapping, actio Nform form, httpservletrequestrequest, httpservletresponse response) Throwsexception {//calling Business 
      Logical operation List userlist = Usermanager.getinstance (). Findalluserlist (); 
       
      Request.setattribute ("UserList", userlist); 
   Returnmapping.findforward ("list_success"); } 
  
   
   /** * User Delete * @param mapping * @param form * @param request * @param response * @return * @throws Exception */public Actionforward del actionmapping mapping, actionform form, Httpservle  Trequestrequest, HttpServletResponse response) throws Exception {//Get the value submitted from the page form Useractionform 
       
      UAF = (useractionform) Form; 
  
      Obtain the set of UserID that need to be removed string[] useridlist = Uaf.getselectflag (); 
      Invoke Business logic Operations Usermanager.getinstance (). Deleteusers (Useridlist); 
   Return Mapping.findforward ("del_success"); 
   /** * User Add * @param mapping * @param form * @param request * @param response * @return * @throws Exception * * * Public actionforward Add (actionmapping Mapping, actionform form, HTTPSERVLETR Equest request, HttpServletResponse Response) throwsexception {//Get the value submitted from the page form Useracti Onform UAF = (useractiOnform) Form; 
      Useruser = new User (); 
      Beanutils.copyproperties (USER,UAF); 
       
      User.setcreatedate (Newdate ()); 
      Invoke Business logic Operations Usermanager.getinstance (). AddUser (user);  Returnmapping.findforward ("add_success"); 
   /** * Modify User * @param mapping * @param form * @param request * @param response * @return * @throws Exception/Public Actionforward Modify (actionmapping mapping, actionform form, Httpservle Trequestrequest, httpservletresponse Response) throwsexception {//Get the value submitted from the page form Useractionform 
      UAF = (useractionform) Form; 
      User user = new user (); 
       
      Beanutils.copyproperties (USER,UAF); 
      Invoke Business logic Operations Usermanager.getinstance (). ModifyUser (user); 
   Returnmapping.findforward ("modify_success");  /** * Query users by ID * * @param mapping * @param form * @param request * @param response * @return * @throws ExceptIon * * Public Actionforward Find (actionmapping mapping, actionform form, Httpservletrequestrequest, Htt Pservletresponse response) Throwsexception {//Get the value submitted from the page form useractionform UAF = (useractionfor 
       
      m) Form; 
       
      String userId = Uaf.getuserid (); 
       
      Invoke Business logic Action User user = Usermanager.getinstance (). Finduserbyid (UserId); 
       
      Pass the user object from the action to the JSP page Request.setattribute ("user", user); 
   Returnmapping.findforward ("find_success"); 
 } 
   
}


The Struts-config.xml configuration file code is shown below.

 <struts-config> <form-beans> <form-bean name= "UserForm" type= "Com.bjpowernode.drp.web.forms.UserActionForm"/> </form-beans> <action-mappings> <action 
           Path= "/user/user_maint" type= "Com.bjpowernode.drp.web.actions.UserAction" Name= "UserForm" Scope= "Request" parameter= "command" > <forward name= "list_success" path= 
        r/user_list.jsp "/> <forward name=" del_success "path="/user/user_maint.do?command=list "true" redirect= <forward name= "add_success" path= "/user/user_maint.do?command=list" redirect= "true"/> <forward n Ame= "modify_success" path= "/user/user_maint.do?command=list" redirect= "true"/> <forward "name=" Path= "/user/user_modify.jsp"/> </action-mappings> </struts-config> 


When the action is configured, the parameter property is configured, and the parameter property value is specified as command, and the user clicks Add or remove user actions to Http://localhost:8080/struts_ Dispatchaction_usermgr/user/user_maint.do?command=list, this request is mapped to the Useraction controller, Struts sends this request to the list method of the controller useraction according to the value of the methods parameter. This gets the call to the parameter completion page.
As you can see from the above, Dispatchaction can decide which method to call dispatchaction by the value of command this parameter, dispatchaction to extract the value of the parameter definition parameter from the URL of the user request To decide which method to call to handle the user request. Therefore, Dispatchaction cannot submit requests to the server through Actionform, because parameters cannot be passed to the server when submitting the form.
Based on the example above we can sum up the difference between dispatchaction and action: The action is to get data from the form and automatically convert to the corresponding type. Instead, dispatchaction obtains the parameter in the configuration file and intercepts the parameter-defined parameter values. But Dispatchaction can handle multiple actions without having to create more than one action.
Dispatchaction can handle multiple actions in the same controller, but only by using a URL to invoke the controller, depending on the parameters submitted by the user to determine which method to process the user request. You cannot submit user request information through a form, and you can use lookupdispatchaction in struts if you want to handle different actions in the same form. Here is not a detailed story, there may be children's shoes can find some information to achieve.

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.