One of struts2 knowledge reviews

Source: Internet
Author: User
Tags i18n
# One of struts2 knowledge reviews

  1. Summary: struts2 is a lightweight web application framework based on the MVC design model. It is essentially equivalent to servlet.
    It is a combination of struts1 (a lightweight Web framework based on MVC model in early days) and webwork (a J2EE Web framework organized by opensymphony) technologies.
    Its core is webwork. It uses an interceptor mechanism to process user requests and disconnects the business logic controller from the servletapi.
  2. Struts2 advantages:
    • Exception Handling Mechanism
    • Provides good Ajax support
    • Result-based page navigation, redirection and page jump through the result tag.
    • Open-source, highly scalable
  3. The web layer frameworks (struts1, struts2, webwork, and springmvc) are all based on the front-end controller mode.
    • What frontend controller mode?
      Overview: in traditional development, each request corresponds to a servlet, which leads to many servlets.
      The struts2 framework filters all requests through a front-end controller and uses the Interceptor to implement some functions,
      Submit the remaining operations to specific actions for processing. Getting started with struts2
      1. Struts2 Development Kit directory
        • Apps: stores struts2 instance programs
        • Docs: struts2 api reference document
        • Lib: Core class library of struts2 and third-party plug-in class library.
        • SRC: struts2 framework source code
      2. Struts2 Development Kit introduced by web engineering
        Overview: in actual development, because we do not need to use all the jar packages under the lib directory, we introduce
        Jar packages under the apps \ struts2-blank \ WEB-INF \ lib directory.
        Struts2 development jar package analysis:
        Struts2-core-2.3.24.jar struts2 core class library
        Xwork-core-2.3.24.jar webwork core class library, the basis of struts2 Construction
        Object graph Navigation language struts2 reads and writes properties of an object through it.
        Class library used by the freemarker-2.3.22.jar struts2 label Template
        Log4j-api-2.2.jar struts2 log management component dependent package APIs
        Log4j-core-2.2.jar struts2 log management component dependency package
        Commons-fileupload-1.3.1.jar struts2 File Upload Component dependency package
        Commons-io-2.2.jar struts2 output input, pass the file dependent jar package
        The commons-lang3-3.2.jar contains some data type tools that are enhancements to the java. lang Package
        Javassist-3.11.0.GA.jar JavaScript bytecode parser
        Asm-3.3.jar operation bytecode class library
        The asm-commons-3.3.jar provides event-based representations. The asm-tree-3.3.jar provides an object-based representation. Struts2 configuration details

        & Basic configuration environment setup
        Struts. xml file constraints:
        <! Doctype struts public
        "-// Apache Software Foundation // DTD struts configuration 2.3 // en"
        Http://struts.apache.org/dtds/struts-2.3.dtd>
        Header label: <struts> </struts>
        & Struts2 execution process:
        Requests sent from the client are first executed by a group of interceptors in the front-end controller (core filter strutsprepareandexecutefilter) to complete some functions.
        After the interceptor is executed, the action is executed, and a result view is returned, which redirects the page according to the result configuration.

      3. Struts. xml file configuration
        (1) constant configuration (use <content> </content>)
        Note: All constant configurations can be found in the/org/Apache/struts2/Default. properties file under the struts2-core-2.3.24.jar package
        Constant configuration attributes:
        Method 1 (based on the Struts. xml file)
        <1> i18n: International encoding, which solves post submission garbled characters.
        Example:
        <Constant name = "struts. i18n. encoding" value = "UTF-8"> </constant>
        <2> struts. Action. Extension = action, which specifies the extension name, action, and default value for accessing the action.
        Action, which indicates that the suffix is action or empty (two symbols indicate null ).
        Example:
        <Constant name = "struts. Action. Extension" value = "action,"> </constant>
        <3> struts. devmode = false (default) Specifies whether struts2 runs in development mode. Development Mode has the following advantages:
        1. Hot loading of the master configuration file (takes effect without restarting)
        2. More error messages are provided to facilitate debugging during development.
        Example:
        <Constant name = "struts. devmode" value = "true"> </constant>
        <4> Configure whether to enable dynamic method call
        Example: struts. Enable. dynamicmethodinvocation = false (default) is disabled by default, and must be manually enabled.
        Analysis:
        After configuration, the browser accesses server data in the format of + after the action class! + Corresponding method name
        For example, http: // localhost/struts2_01/dynamic/demo1action! Query
        This kind of dynamic method constant configuration will lead to weird address bar names, which is not conducive to search by SEO search engine
        Method 2 (based on the Web. xml file)
        Configure struts2 encoding to UTF-8 for the entire web application.
        <Context-param>
        <Param-Name> struts. i18n. Encoding </param-Name>
        <Param-value> UTF-8 </param-value>
        </Context-param>
        Method 3 (override a constant configuration file under SRC. Default. properties overwrites the existing constant configuration file of the Framework)
        (2) Basic Configuration
        <Package name = "" namespace = "" extends = "struts-Default">
        <Action name = "" class = "" method = "">
        <Result name = "success">/xxx. jsp </result>
        </Action>
        </Package>
        Details:
        <1> the parent label package encapsulates multiple actions in one package. Its Attributes are as follows:
        • Name: indicates the package name. It can be used but cannot be repeated.
        • Namespace: defines a namespace for the action access path.
        • Extend: inherits all the configurations of a specified Struts-default package under the struts2 framework.
          This attribute identifies whether the package is abstract. If it is true, it indicates that the package cannot run independently, but it is only inherited
          <2> Level 1 sub-tag <action>: configure the action class. Its Attributes are as follows:
          • Name: determines the resource access name of the action.
          • Class: complete Class Name of action
          • Method: Call the method in the actionl class to process the request.
            <3> Level 1 subtag <default-action-ref>
            <Default-action-ref name = "XX. dd. demo2action"> </default-action-ref>
            Demo2action is used as the default action to process the request when the access request cannot find the ation under the package.
            <3> second-level sub-tag <result>, result Configuration
          • Name attribute: name of the processing result, which corresponds to the return value of the action method.
          • Type attribute: Specifies the result class to be called to process the result. Forwarding is used by default for result processing.
            & Result processing method:
            1 type = "dispatcher" request forwarding
            2 type = "Redirect" redirection
            3 redirect to another action (commonly used) Demo1action / $ {Name} 4. forward requests to another action (not commonly used) Demo1action / & TAG body content: Enter the relative path of the page to jump to <4> Level 2 sub-tag (3) introduce other struts. xml files in the SRC path in the following format: (4) configure the interceptor in the following format: Add, delete
      4. Configure core Filters
        Overview: All Web frameworks have features based on the front-end controller (core filter) mode.
        Example: Struts2 Org. Apache. struts2.dispatcher. Ng. Filter. strutsprepareandexecutefilter Struts2 /*
      5. Struts2 configuration file loading sequence
        Overview: each time a request is sent from a client to a server, the request passes through the struts2 core filter strutsprepareandexecutefilter, which provides preprocessing and execution functions.
        • Preprocessing is mainly used to load the configuration file, corresponding to the init () method of the filter.
        • Execute the dofilter () method that is used to execute a group of interceptors to complete some functions.
          The loading sequence of struts2 configuration files in the dofilter () method is described as follows:
          (1) first load the Struts. xml file, corresponding to the Configuration action and constant.
          (2) load the Struts. properties file and configure constants accordingly.
          (3) load the Web. xml file and configure the core filter and constant. Write the action class

          1 Overview: action is the core class of the framework and also the business logic controller to process user requests. Each request corresponds to an independent action class unit.
          The action class is a pojo class (plain ordinary Java object simple Java class) which has a part of the getter/setter method and does not inherit any parent class, and does not implement any interfaces.
          There is a common construction without parameters (default) and an execute method (public permission modifier, string return type, method is null parameter ).
          The pojo class that meets the preceding requirements can be counted as the action class implementation. In addition, the struts2 framework provides other implementation methods of the action class.
          2. Implementation of the action class
          (1) Method 1: implement the action (COM. opensymphony. xwork2.action) Interface
          Sample Code:
          Public class helloaction implements action {br/> @ override
          System. Out. println ("Hello world! ");
          Return "success ";
          }
          }
          However, this interface defines an execute method and five basic constants:

        • Sucess jumpNoneError jump to error handling page
        • Path to jump during input data verificationLogin jumps to the logon page
          It provides little help for developers, and generally does not need to be used for actual development. Instead, it uses the inherited actionsupport class.
          (2) Method 2: Inherit the actionsupport class
          Sample Code:
          Public class helloaction extends actionsupport {
          }
          The actionsupport class implements the action interface and other interfaces to provide more functions for users.
          In fact, actionsupport is struts2's default action processing class. inheriting this class can simplify action development.
          3. Action class access
          (1) use wildcards
          Configure the dynamic action method (resolve an action class to process multiple requests in a module)
          Example:
          <Action name = "demo1action* "Class =" XXXX. demo1action "method =" {1} ">
          <Result name = "success">/Hello. jsp </result>
          </Action>
          Analysis:
          Demo1action
          OfIs the method name of the action class, which corresponds to 1 in method = "{1}" (retrieve the content of the first wildcard,
          If demo1action** Method = "{2 }"
          Extension: It is worth mentioning that <constant name = "struts. Enable. dynamicmethodinvocation" value = "false"> </constant>
          Constant configuration is another way to implement the action method.

One of struts2 knowledge reviews

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.