Struts2 Framework Learning (iii) data processing

Source: Internet
Author: User

STRUTS2 Framework Framework uses OGNL language and value stack technology to achieve data flow processing.

The value stack is the equivalent of a container for storing data, while OGNL is a language for quickly querying data.

Value stack: Valuestack a data structure that operates on the following: Advanced

Ognl:object-graphnavigation Language (Object Graphics navigation language) a tree-like structure is used to illustrate the relationship of multiple objects, and a graph is more likely to be used if you need to manipulate the tree-structured node data. Properties , the OGNL technology is implemented using reflection in the bottom of the system.

One: How the data is submitted

1,<form> form Submission

<form action= "/user/login_login.action" method= "POST" >
    user code: <inputname= "Usercode" type= "text" > <br>
    user password: <inputname= "userpswd" type= "password" ><br>
              <inputtype= "Submit" value= "Login" >

The way of 2,url


3, hyperlinks

<a href= "/user/delete.action?usercode=admin" > Delete </a>  

4, asynchronous commit

Ajax asynchronously submits data

Two: Data storage

The storage of the data relies on the interceptor functionality provided by the framework, which intercepts the request, obtains all the request parameters, and loops through the set into the value stack. The framework by default stores the requested action object to the top of the stack of values. The STRUTS2 framework provides three ways to store parameters in the value stack

1, attribute-driven mode

The action requires a set method that provides the name of the parameter, and the framework obtains the request parameters through the interceptor, looping through the parameters to the value stack (the top object of the stack).

<input name= "Usercode" type= "text" >
ognl.setvalue ("Usercode", Action, "admin");//This time the root object is action

2, model-driven mode

The model-driven interface must be implemented, which is an intrusive development method and is not recommended for use

You need to define a class for the data model, encapsulate the attributes into a data model class, and only define the type properties of the model object (you must create an object assignment), and the Get/set property of the model object is not required.

The action class needs to implement the Modeldriven interface, overriding the Getmodel () method.

<inputname= "Usercode" type= "text" ><br> form is still the property name of the model object as the parameter name
ognl.setvalue ("Usercode", User, "admin ");//This time the root (top) object is the user object

3, domain-driven mode

The principle of property-driven is similar, and is also through the parameter interceptor, the request parameter gets after the loop is set to the value stack.

Defines the model object properties in the Action object and provides the Get/set method;

Adds the name of the Model object property to the form element;

<inputname= "User.usercode" type= "text" >
ognl.setvalue ("User.usercode", Action, "admin");//This time the root object is action

Third: Transfer of data

The framework stores both the HTTP object and the wrapped Map collection object in the Actioncontext context object collection. So you can get the object you want to use according to Actioncontext.

1, get the HTTP object

    Actioncontext AC = Actioncontext.getcontext (); The context object corresponds to the request range
(1) HttpServletRequest request = (httpservletrequest) ac.get (strutsstatics.http_request);
    HttpSession session = Request.getsession (false);
    ServletContext application = Session.getservletcontext ();
    ServletContext application = Ac.get (Strutsstatics.servlet_context);
(2) HttpServletRequest request = Servletactioncontext.getrequest (); (Recommended use)
    HttpServletResponse response =servletactioncontext.getresponse ();
(3) Action class implementation Servletrequestaware, or Servletresponseaware (belongs to the intrusive development method, not recommended)
    private HttpServletRequest Request  ;//set inject
    private httpservletresponse response;
    @Override public
    void Setservletrequest (HttpServletRequest request) {  //implements the method, which is called by the framework to pass parameters.
         this.request= request;
    @Override public
    void Setservletresponse (httpservletresponse response) {
        this.response= response;

2, get the map collection

  Actioncontext AC = Actioncontext.getcontext ();
  Mapsession = Ac.getsession ();
  Mapsession2 = (MAP) Ac.get ("session");
  mapsession3= (MAP) ac.get (actioncontext.session);        
  Mapapplication = Ac.getapplication ()
  mapapplication = Ac.get (actioncontext.application);
  Mapapplication = Ac.get ("Application");

3, get the value stack object and the Parameter collection object

  Actioncontext AC = Actioncontext.getcontext ();
  Valuestack vs = Ac.getvaluestack ();
  Map Paramts = Ac.getparameters ();

Four: the display of data

Take the value from the Request object in the form of an El expression

  and the way this expression can be represented as Java code:
    stringusername = (String) Request.getattribute ("username");//overridden method, underlying is the data
    Out.print (username) in the OGNL query value stack;

  returns data from request, essentially underlying the data in the value stack through the OGNL language. The collection of  actioncontext objects and the collection of Ognlvaluestack value stacks are the same collection object, so fetching data from the map collection of the value stack is equivalent to fetching the data from the Actioncontext context.

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: 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.