Struts2 knowledge Review 3

Source: Internet
Author: User
Struts2 MVC idea and AOP-oriented aspect Programming

1. MVC Overview:
M model (business logic model, service layer, Dao layer)
V view (view, HTML, CSS, JS page)
C controller (front-end controller, Servlet, JSP, Action)
2 struts2mvc process:
When the client sends a request, the core filter of struts2 intercepts the request and passes the request to action-controller.
Call the service and Dao-layer business method in action --- Model
Return result of action, jump to page -- View
3. AOP aspect Programming
Simplified description: repeats code vertically and extracts data horizontally.
Typical applications:

  • In the servlet era, filters are used to uniformly process the garbled characters of multiple Servlet classes.
  • In the action era, interceptor is used to pass the same parameters to multiple different actions.

    Struts2 and ognl (default struts Expression Language)

    1 Overview: ognl (object graph Navigation language) is an open source Expression Language.
    It can access the attributes of Java objects and call methods of Java objects through expression syntax, and automatically implement type conversion.
    2 features:

    (1) supports object method calls. For example, objectname. method () (2) supports class static method calls and value access. Format: @ full class name @ method name | value name, for example, [email protected] (xx) (3) value assignment and expression concatenation are supported. (4) accessible ognl context objects ognl context and action context objects actioncontext (5) operable set objects

    3. ognl details
    (1) Overview: The ognl expression language structure consists of three elements: expression, root object, and context ).
    <1> ognl expressions (specifying what ognl does)
    & It is the core of ognl, and all ognl operations are performed on expressions.
    & It is a string with syntax meaning, which specifies the operation type and content.
    & It not only supports chained object access paths, but also supports Simple Computation in expressions.
    <2> root object of ognl (WHO to operate on)
    & It is an ognl operation object. It can use any object as the root and access other objects associated with this object through ognl.
    & Set the root object. ognl can perform any write operations on the root object.
    <3> context object (specifying where to operate)
    & The environment where the root object is located is the context object of ognl.
    & The context object context is a map object. When an expression is used to access an object in the context,
    You must add the object name with the # sign.
    (2) ognl APIs
    <1> Create ognlcontext and root objects
    // Create a user object as the data of the root object
    User rootuser = new user ("Tom", 18 );
    // Create a map set as the data of the ognlcontext object
    Map <string, user> map = new hashmap <string, user> ();
    Map. Put ("user1", new user ("Jack", 18 ));
    Map. Put ("user2", new user ("Rose", 19 ));

    // Create ognlcontext object ognlcontext OC = new ognlcontext (); // encapsulate map data into ognlcontext oC. setvalues (MAP); // encapsulate the rootuser data to the root oC. setroot (rootuser); <2> get the root object and other object data of ognlcontext. // Obtain the entire root Object User Root = (User) ognl in ognlcontext. getroot (OC); // obtain the value string name = (string) ognl of the name and age of the root object in ognlcontext. getvalue ("name", OC, OC. getroot (); int age = (INT) ognl. getvalue ("Age", OC, OC. getroot (); system. out. println ("root:" + root + ", rootname:" + name + ", rootage:" + age ); // obtain data from other ognlcontext objects // obtain the ognlcontext object user1 = (User) ognl. getvalue ("# user1", OC, OC. getroot (); // get ognlco The name and age attributes of the user1 object in ntext string user1_name = (string) ognl. getvalue ("# user1.name", OC, OC. getroot (); integer userprivilege age = (integer) ognl. getvalue ("# user1.age", OC, OC. getroot (); system. out. println ("user1:" + user1 + ", user1name:" + user1_name + ", user1age:" + usermetadata age); <3> modify the root object and other object data of ognlcontext. // Modify the name and age attribute data of the root object in ognlcontext. getvalue ("name = 'flower'", OC, OC. getroot (); ognl. getvalue ("age = 100", OC, OC. getroot (); // get the modified root Object User Root = (User) ognl. getroot (OC); system. out. println ("root:" + root); // modify the name and age attributes of user1 of other objects in ognlcontext: String user1_name = (string) ognl. getvalue ("# user1.name = 'panda'", OC, OC. getroot (); integer userprivilege age = (integer) ognl. getvalue ("# user1.age = 200", OC, OC. getroot (); // get the modified user1 Object User user1 = (User) ognl in the context. getvalue ("# user1", OC, OC. getroot (); system. out. println ("user1:" + user1); <4> call object method for ognl operations // call ognl for the setname method of the root object and user1 object in ognlcontext. getvalue ("setname ('root111111')", OC, OC. getroot (); ognl. getvalue ("# user1.setname ('u1111111')", OC, OC. getroot (); // get the root object after the method is used and the user1 Object User Root = (User) ognl. getroot (OC); User user1 = (User) ognl. getvalue ("# user1", OC, OC. getroot (); system. out. println ("root:" + root); system. out. println ("user1:" + user1 ); <4> call static methods of the ognl operation tool class // The custom tool class myutils is as follows: public class myutils {// echo method public static object echo (Object O) {return O ;}} // call the custom tool class static method format: @ + complete class name [email protected] Static Method string value = (string) ognl. getvalue ("@ [email protected] ('Hello Flower')", OC, OC. getroot (); system. out. println (value); // call the static property PI double Pi = (double) ognl of the jdk built-in tool class math. getvalue ("@ [email protected]", OC, OC. getroot (); double Pi2 = (double) ognl. getvalue ("@ pi", OC, OC. getroot (); // abbreviated as system. out. println (Pi2); <5> the gonl operation stores the object data of a single column set in the list to the ognlcontext, note that # is not used here. // create a list set object in ognlcontext. Note that more than one element letter can recognize strings. Otherwise, ognl is used as a character set. getvalue ("{'A', 'bb ', 'cc'}", OC, OC. getroot (); // re-obtain the entire list set object and other data list of the set from ognlcontext <string> List = (list <string>) ognl. getvalue ("{'A', 'bb ', 'cc'}", OC, OC. getroot (); integer size = (integer) ognl. getvalue ("{'AA', 'bb ', 'cc '}. size () ", OC, OC. getroot (); string name1 = (string) ognl. getvalue ("{'AA', 'bc', 'cc'} [0]", OC, OC. getroot (); string name2 = (string) ognl. getvalue ("{'AA', 'bc', 'cc '}. get (0) ", OC, OC. getroot (); system. out. println ("list:" + list + "; \ n" + "listsize:" + size + "; listname1:" + name1 + "; listname2:" + name2 ); <6> the gonl operation stores the map double row set object data to the ognlcontext. Note that # is used to create a map double row set. # is used to identify ognl. getvalue ("# {'name': 'zs', 'age': 18}", OC, OC. getroot (); // obtain the data hashmap <string, Object> map = (hashmap <string, Object>) ognl of the entire map set and map set. getvalue ("# {'name': 'zs', 'age': 18}", OC, OC. getroot (); integer size2 = (integer) ognl. getvalue ("# {'name': 'zs', 'age': 18 }. size () ", OC, OC. getroot (); string name3 = (string) ognl. getvalue ("# {'name': 'zs', 'age': 18} ['name']", OC, OC. getroot (); string name4 = (string) ognl. getvalue ("# {'name': 'zs', 'age': 18 }. get ('name') ", OC, OC. getroot (); system. out. println (MAP + "\ n" + size2 + ";" + name3 + ";" + name4 );

    (3) ognl Summary
    Ognl Expression Language, which runs in a context object environment ognlcontext,
    The environment consists of a root object and a context of the map set type.

    Struts2 value stack (valuestack)

    1 Overview
    (1) generation of value stack valuestack:
    To run an ognl expression, you must have the ognl environment ognlcontext.
    Struts2 generates an ognlcontext named valuestack in combination with the ognl technology.
    Valuestack is composed of two parts, one of which is root, which is a stack,
    The other part is the actioncontext data center, which is a map set.
    (2) value stack features:
    & Value stack valuestack is an interface of struts2, and ognlvaluestack is an implementation class of valuestack.
    & When the client sends a request, struts2 creates an action instance and an ognlvaluestack value stack instance.
    & Struts2 accesses and reads and writes action parameter data through ognl in the value stack.
    2. Internal Structure of ognlvaluestack
    (1) Overview: ognlvalue includes two parts: Value stack (compoundroot) and Map <string, Object> context (ognl context object ognlcontext ).
    (2) compoundroot details
    <1> It is the root object of ognlcontext and stores the action instance data.
    <2> it inherits the features of the arraylist to implement the stack pressure and stack exit functions. It is an object stack.
    The action instance in compoundroot is located at the top of the stack. struts2 first searches for corresponding attributes from the action instance at the top of the stack,
    If not, find other objects from the top of the stack.
    <3> compoundroot (custom stack) is an improvement for the use of ognl root by struts2,
    Call the findvalue () method of ognlvaluestack to find the objects in the compoundroot stack and their attributes.
    (3) ognlcontext
    <1> It Is A Map <string, Object> structure that stores references, parameters, request, session, application, ATTR, and so on.
    <2> some references of ognl
    & Parameters: this map is the request parameter of the current request.
    & Request: the map is the data in the request field.
    & Session: the map is the data in the session field.
    & Appliaction: the map is the data in the application field.
    & ATTR: the map retrieves the above objects, request, session, and application in the following order.
    3 valuestack
    (1) Relationship between actioncontext and valuestack

    • Struts2 creates an actioncontext and a valuestack value stack object.
    • There is a valuestack reference in actioncontext, and an actioncontext reference in valuestack.
    • Actioncontext obtains the value stack of the servletapi dependency valuestack.
      (2) valuestack API operations
      <1> get value stack object
      • Method 1 (via actioncontext ):
        Valuestack = actioncontext. getcontext (). getvaluestack ();
      • Method 2 (via servletactioncontext ):
        String Vs = servletactioncontext. struts_valuestack_key;
        Valuestack = (valuestack) servletactioncontext. getrequest (). getattribute ();
        <2> operation value stack object
      • Method 1: Provide the get method in action
        Because action itself is in the value stack, you can use the get () method of the attribute to obtain the attribute value of action from the value stack.
      • Method 2: manually operate the value Stack
        Call the push and set methods of the value stack to manually operate the data in the value stack.
        <3> struts2 El expression
      • # Usage
        & Get the data in the context object ognlcontext in the value stack, such as the request data
        Example: <s: property value = "# request. Name"/>
        & Traverse the set value
        <S: iterator value = "# {'aaa': '000000', 'bbb': '000000'}" Var = "entry"> ------------------------------
      • %
        & Forced parsing of ognl expressions
        <S: textfield name = "name" value = "% {# request. name}"> </S: textfield>
        & Force the ognl expression not to be parsed
        <S: property value = "% {'# request. name'}"/>
      • $
        Used to write an ognl expression in the struts2 configuration file. How struts2 encapsulates data with ognl Technology

        1. Property-driven
        (1) the client sends request parameters. The param interceptor of struts2 intercepts the parameters, and the ognl processes the parameters.
        (2) ognl first obtains the relevant attributes of the Action instance from the root stack of the valuestack value stack and assigns a value to this attribute.
        2. Object-driven
        (1) the client sends request parameters. The param interceptor of struts2 intercepts the parameters, and the ognl processes the parameters.
        (2) ognl first obtains the related objects in the Action instance from the root stack of the valuestack value stack and assigns a value to the object attribute.
        3. model-driven
        (1) the client sends request parameters. The param interceptor of struts2 intercepts the parameters, and the ognl processes the parameters.
        () Ognl first obtains the related objects in the Action instance from the root stack of the valuestack value stack and assigns a value to the object attribute.

Struts2 knowledge Review 3

Related Article

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.