Progressive struts1 (7) Detailed description of dispatchaction

Source: Internet
Author: User

Through the previous blogs, I don't know if you have found this problem. Although you can control the redirection flexibly now, there are still a large number of actions. How can we ensure flexible jump, can the number of actions be reduced? This is what we call dispatchaction in this blog. Its name can be understood as "distributed action", which can avoid creating a class for each action.

Let's take a look at the instance.

Useraction

Dispatchaction inherits action, which combines multiple previous actions into one. When multiple actions are associated with a large number of actions, they can be put together like this to reduce the number of action classes at the same time, it also reduces the difficulty of maintenance.

Package COM. tgb. DRP. web. actions; import Java. util. date; import Java. util. list; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. commons. beanutils. beanutils; import Org. apache. struts. action. actionform; import Org. apache. struts. action. actionforward; import Org. apache. struts. action. actionmapping; import Org. apache. struts. actions. dispatchaction; import COM. tgb. DRP. manager. usermanager; import COM. tgb. DRP. model. user; import COM. tgb. DRP. web. forms. useractionform; public class useraction extends dispatchaction {@ brief actionforward unspecified (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {// call the business logic operation list userlist = usermanager. getinstance (). findalluserlist (); Request. setattribute ("userlist", userlist); Return Mapping. findforward ("list_success ");} /*** Delete ** @ Param mapping * @ Param form * @ Param Request * @ Param response * @ return * @ throws exception */Public actionforward del (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {// get the value submitted from the page form useractionform UAF = (useractionform) form; // obtain the set of userids to be deleted string [] useridlist = UAF. getselectflag (); // call the business logic operation usermanager. getinstance (). deleteusers (useridlist); Return Mapping. findforward ("del_success ");} /*** add * @ Param mapping * @ Param form * @ Param Request * @ Param response * @ return * @ throws exception */Public actionforward add (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {// get the value submitted from the page form useractionform UAF = (useractionform) form; user = new user (); beanutils. copyproperties (user, UAF); User. setcreatedate (new date (); // call the business logic operation usermanager. getinstance (). adduser (User); Return Mapping. findforward ("add_success ");} /*** modify user * @ Param mapping * @ Param form * @ Param Request * @ Param response * @ return * @ throws exception */Public actionforward modify (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {// get the value submitted from the page form useractionform UAF = (useractionform) form; user = new user (); beanutils. copyproperties (user, UAF); // call the business logic operation usermanager. getinstance (). modifyuser (User); Return Mapping. findforward ("modify_success ");} /*** query the user by ID ** @ Param mapping * @ Param form * @ Param Request * @ Param response * @ return * @ throws exception */Public actionforward find (actionmapping Mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {// get the value submitted from the page form useractionform UAF = (useractionform) form; string userid = UAF. getuserid (); // call the business logic operation user = usermanager. getinstance (). finduserbyid (userid); // pass the user object from action to the JSP page request. setattribute ("user", user); Return Mapping. findforward ("find_success ");}}

Useractionform

To encapsulate data in a form, you do not need to create an actionform for each form. Multiple forms can be encapsulated using one.

Package COM. tgb. DRP. web. forms; import Java. util. date; import Org. apache. struts. action. actionform;/*** user management actionform **/public class useractionform extends actionform {// user code private string userid; // user name private string username; // password private string password; // contact number private string contacttel; // emailprivate string email; // creation date private date createdate; // Private string [] selectflag; Public String getcontacttel () {return contacttel;} public void setcontacttel (string contacttel) {This. contacttel = contacttel;} public date getcreatedate () {return createdate;} public void setcreatedate (date createdate) {This. createdate = createdate;} Public String getemail () {return email;} public void setemail (string email) {This. email = Email;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} Public String getuserid () {return userid;} public void setuserid (string userid) {This. userid = userid;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String [] getselectflag () {return selectflag;} public void setselectflag (string [] selectflag) {This. selectflag = selectflag ;}}

Struts-config

Configure action and actionform. different from the previous one, you need to add the parameter attribute.

    <form-beans><form-bean name="userForm" type="com.tgb.drp.web.forms.UserActionForm"/>    </form-beans>        <action-mappings>    <action path="/user/user_maint"    type="com.tgb.drp.web.actions.UserAction"    name="userForm"    scope="request"    parameter="command"    >    <forward name="list_success" path="/user/user_list.jsp"/>    <forward name="del_success" path="/user/user_maint.do" redirect="true"/>    <forward name="add_success" path="/user/user_maint.do" redirect="true"/>    <forward name="modify_success" path="/user/user_maint.do" redirect="true"/>    <forward name="find_success" path="/user/user_modify.jsp"/>         </action><action path="/user/show_add"forward="/user/user_input.jsp"></action>    </action-mappings>

Access interface

<Body> <a href = "User/user_maint.do" Title = "click to access the user management system"> User Management System </a> </body>

As shown above, it is determined based on the value of the command attribute.

Execution Process

Set the breakpoint mainly in dispatchaction. the execution process is as follows:

Code Analysis

The execute function of dispatchaction contains the following code:

// Prevent recursive callsif ("execute".equals(name) || "perform".equals(name)){String message =messages.getMessage("dispatch.recursive", mapping.getPath());log.error(message);throw new ServletException(message);}

Therefore, the parameter value cannot be execute or perform.

The following code is available in dispatchmethod of dispatchaction:

        if (name == null) {            return this.unspecified(mapping, form, request, response);        }


That is, if the parameter value is null, the unspecified function is executed. You can implement this function in the subclass as a jump without parameter values.

Summary

All in all, the appearance of dispatchaction reduces the number of actions and integrates multiple actions. Although there are obvious advantages, there will also be problems. This coupling improves, it is inevitable that the scalability is reduced, so it is not necessary to use it, but also depends on the specific situation.

For more related blogs, go to the Summary of progressive struts1 (8)

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.