Open-source MVC Framework struts (2) Analysis of struts Workflow

Source: Internet
Author: User
  1. Introduction

    1. Our daily life is well organized, and our daily life is quite regular. For example, when we get up, we have formed a "biological clock ".
    1. In the factory, the workers work hard on the assembly line, and each link constitutes the entire workflow. to do one thing well, a complete workflow is required, beginning and end.
    2. Struts has designed a standard workflow for the web layer of our project for us to follow. That is, if you use the Struts framework, you have to follow the rules of others, let's analyze the struts workflow.
  1. Flowchart

    1. First, let's look at a flowchart.

      1. The procedure is as follows:

        1. When Tomcat is started, information from the Web. XML, Struts-config.xml configuration file is first loaded into the memory, Struts-config.xml corresponding file.
        1. The login page requests the server in the form of login. Do. The actionservlet will forward this request to the corresponding action for processing.

          1. Actionservlet inherits httpservlet and implements httpservlet.
        1. The action calls the model layer and returns the result according to <forward
          /> The tag configuration is returned to the specified page.
        2. Actionmapping/formbean is not drawn, they are the objects that save the configuration file information and page data, also play a very important role.
  1. Dispatch and dynaactionform

    1. Dispatch

      1. You usually need to write an action class for each processing page for control, but there will be a lot of problems if there are too many action classes on the page.
      2. Dispatch moves the processing process of each action to each method. You only need to write an action.
    1. Dynaactionform

      1. Actionform corresponds to the attributes on the form, and each variable must be the same as the attribute name on the form. When the form attribute is added or deleted, the actionform content needs to be modified and the program needs to be re-compiled, the introduction of dynamic actionform solves this problem and requires simple configuration.
  1. Dispatch example

    1. The following is a page instance for adding and deleting materials. The call process is as follows.
    2. Jsp request page
      1. <Body> <form> <a href = "item. do? Command = Add "> item </a> <a href =" item. do? Command = del "> item </a> </form> </body>
    1. Dispatch action

      1. Package COM. bjpowernode. DRP. web. actions; import Java. io. fileoutputstream; 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 Org. apache. struts. upload. formfile; import COM. bjpowernode. DRP. beanfactory; import COM. bjpowernode. DRP. pagemodel; import COM. bjpowernode. DRP. domain. item; import COM. bjpowernode. DRP. domain. itemcategory; import COM. bjpowernode. DRP. domain. itemunit; import COM. bjpowernode. DRP. service. datadictservice; import COM. bjpowernode. DRP. service. itemservice; import COM. bjpowernode. DRP. web. forms. itemactionform; public class itemaction extends dispatchaction {/*** 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 {item = new item (); itemactionform iaform = (itemactionform) form; beanutils. copyproperties (item, iaform); itemcategory = new itemcategory (); itemcategory. setid (iaform. getcategory (); item. setitemcategory (itemcategory); itemunit = new itemunit (); itemunit. setid (iaform. getunit (); item. setitemunit (itemunit); itemservice = (itemservice) beanfactory. getinstance (). getbean (itemservice. class); itemservice. additem (item); Return Mapping. findforward ("item_index ");} /*** 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 {itemactionform = (itemactionform) form; itemservice = (itemservice) beanfactory. getinstance (). getbean (itemservice. class); itemservice. delitem (itemactionform. getselectflag (); Return Mapping. findforward ("item_index ");}}
    1. Struts-config.xml Configuration

      1. <? XML version = "1.0" encoding = "ISO-8859-1"?> <! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.2 // en" http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd "> <Struts-config> <form-beans> <form-bean name = "itemform" type = "com. bjpowernode. DRP. web. forms. itemactionform "/> </form-beans> <! -- The parameter value must be the same as the request's command name --> <action-mappings> <action Path = "/item" type = "com. bjpowernode. DRP. web. actions. itemaction "name =" itemform "Scope =" request "parameter =" command "> <forward name =" item_index "Path ="/item. do "Redirect =" true "/> </Action-mappings> <message-resources parameter =" messageresources "/> </Struts-config>
    1. Note: 1. The command = add of the page request. The value after this parameter must be the same as the method name in the dispatch action to correspond to the corresponding method. 2. The parameter = command property needs to be configured in the Struts-config.xml.

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.