Struts2 operation data in the Action

 

Servlet storing data mode

In the Servlet ServletContext object used to store data across the WebApp, ServletContext WebApp directly store the entire public data, using set | get | removeAttribute () to manipulate data.

In addition there are three types of ServletContext many small objects:

  • A storage ServletConfig ServletConfig a Servlet initialization configuration parameters
  • request a request parameter storage request of a user
  • Storing a session of a user session information of the session

This class object 3 is also used to store data, also has a corresponding set | get | removeAttribute () method.

A total of four types of objects, which are four categories of objects to store data using the Map, the Map is called domain.

 

 

 

 

Action mode of operation data

Action is used instead of the Servlet, Servlet ServletContext use to store data, Action by ActionContext to store data.

Context = the ActionContext ActionContext.getContext (); 

        // set, modify, obtain parameters 
        context.put ( "Age", 18 is );
         int Age = ( int ) context.get ( "Age");    // return value is Object, require strong turn

 

 

ActionContext itself can store data, also has other ActionContext domain objects.

Context = the ActionContext ActionContext.getContext (); 
        
        Map <String, Object> = context.getApplication application (); // Get Map object ServletContext, i.e. application domain 
        Map <String, Object> session = context.getSession (); // Get Map object session object, i.e. the domain session 
        HttpParameters parameters = context.getParameters (); // get all request parameters, this object is equivalent to a built-in EL param, params objects

Map object, HttpParameters objects are put (), get (), remove () method, the data may be operated by these methods.

 

 

 

 

 

Get Servlet native objects, the object operated by a native data Servlet

Struts2 provides three ways to get Servlet objects native.

1, ActionContext itself may store data, it may be acquired ServletContext, request, response Servlet native three objects:

 ActionContext context = ActionContext.getContext();

        //获取原生的Servlet对象。因为get()返回值是Object,所以均需强转
        ServletContext servletContext = (ServletContext) context.get("com.opensymphony.xwork2.dispatcher.ServletContext");
        HttpServletRequest request = (HttpServletRequest) context.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
        HttpServletResponse response = (HttpServletResponse) context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");

 

 

2, key too complicated, remember, so Struts2 provides tools ServletActionContext:

//均为ServletActionContext类的静态方法
        ServletContext servletContext = ServletActionContext.getServletContext();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();

 

 

3, to get by implementing an interface

public class LoginAction extends ActionSupport implements ServletContextAware {

    @Override
    public String execute() throws Exception {
        return super.execute();
    }

    @Override
    public void setServletContext(ServletContext servletContext) {

    }
}

ServletContextAware implement an interface, only need to implement a method. Tomcat will obtain and pass a ServletContext object, we have to do is to write a member variable of type ServletContext, the passed value assigned to it, so this Action can be used directly in the ServletContext object (member variables).

public class LoginAction extends ActionSupport implements ServletContextAware {
    private ServletContext servletContext;

    @Override
    public String execute() throws Exception {
        return super.execute();
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;
    }
}

 

To use the ServletContext object, you realize ServletContextAware interfaces;

To use the HttpServletRequest object, you realize ServletRequestAware interfaces;

To use HttpServletResponset object, you realize ServletResponseAware interfaces;

These interfaces can be achieved simultaneously.

 

In fact, the bottom of the second and third ways are: a way to get the first call Servlet objects native, returned.

 

 

 

 

 

ActionContext object life cycle

  •  Servlet is not thread-safe. During the WebApp to run only one instance of a Servlet, all requests (users) to share a member variable of this instance, it is easy to cause problems, it is common practice to variable may be modified by multiple users write in the body of the method (local variables) . Servlet-based Struts1 unsafe.

  • Struts2 is thread-safe. During the WebApp run, Struts2 will be created for each request a new Action instance, processing this request, Action instance corresponding to destruction.

  • ActionContext objects created with the Action object is created, with the destroyed Action object and destroyed. That is consistent with the life cycle of the request object and Servlet ActionContext natively, it may be used to store data instead of ActionContext request the Servlet. ActionContext can communicate data, but does not request parameter in the data region ActionContext not ActionContext.getContext (). Get ( "xxx") this request to obtain the parameters.

 

 

 

 

 

Action parameter acquisition request in a manner

 1, access request object native to the acquisition request through the request object parameter

HttpServletRequest request = ServletActionContext.getRequest();
String name = request.getParameter("name");

 

 

2, the object acquired by the ActionContext HttpParameters, then the acquisition request to the object parameters HttpParameters

ActionContext context = ActionContext.getContext();
        HttpParameters parameters = context.getParameters();
        Parameter name = parameters.get("name");

Parameter Parameter type is obtained. HttpParameters equivalent to a Map.

 

 

 

3, attributes drive

public  class the LoginAction the extends ActionSupport {
     Private String name;
     Private Integer Age;   // Although automatic unpacking, packing, or try to use the type of packaging 

    public String getName () {
         return name; 
    } 

    public  void the setName (String name) {
         the this . = name name; 
    } 

    public Integer getAge () {
         return Age; 
    } 

    public  void the setAge (Integer Age) {
         the this .age = Age; 
    } 

    @Override 
    public String execute() throws Exception {
        return null;
    }

}

Request parameters to be used written Action member variables, and provide a setter, getter methods, and configured to provide a reference empty Action.

Request parameter of type String, will first request the conversion parameters corresponding member variable type, then call setter methods to assign corresponding member variables.

It applies only to eight kinds of basic type, Date type, Date string format types are required, such as 2019-09-01.

Required request parameters name (form field name) and the variable name is the same as member variables.

    name:<input type="text" name="name"><br />
    age:<input type="text" name="age"><br />

Attribute-driven Cons: request parameters to be used very often, Action there will be a lot of member variables, getter, setter methods, very complicated.

 

 

 

 

4, the target drive

The request parameters to be used to package a JavaBean (entity class), as a member variable of Action, to provide the corresponding Action getter, setter and an empty argument constructor method:

public class LoginAction extends ActionSupport {
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(user.getName());
        return null;
    }

}

 

 

Desirable to provide JavaBean (based entity), the entity class to have getter, setter method, an empty argument constructor:

public class User {
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

 

 

The name attribute of the form fields to add name of the object:

 name:<input type="text" name="user.name"><br />
 age:<input type="text" name="user.age"><br />

Written the name attribute different from before, it may not be used.

 

 

 

 

5, model-driven

public  class the LoginAction the extends ActionSupport the implements ModelDriven <the User> {
     // need to explicitly create a (new up), otherwise it is null. 
    = the User new new User Private the User ();
 
    @Override
     public the User getModel () { 
        // return object accepts the request parameters return User; 
    } 
    @Override public String Execute () throws Exception {
         return null ; 
    } 
}
        

     

Need to achieve ModelDriver <T> interface simply implement getModel () method returns the object accepts the request parameters.

Accept the need to explicitly create an object request parameters, otherwise the object is null, the use of this object throws a null pointer exception. But do not give this object provides setter, getter method.

 

Still we need to provide a JavaBean (entity classes).

 

The name attribute of the form fields using the original wording:

    name:<input type="text" name="name"><br />
    age:<input type="text" name="age"><br />

 

Model-driven disadvantages: only one Model, only the request parameters to a package entity classes. Target drive may be a plurality of objects, the parameters may be packaged into a plurality of objects.

 

 

 

 

6, using the parameter set encapsulated request

Servlet is native to the request in the request parameter package, the package is HttpParameters to HttpParameters request parameters, the request attribute is a parameter driven packaged into multiple Action member variable, the target drive, drive model encapsulates all request parameters a JavaBean.

Further use can also be set (List or Map) encapsulated request parameters.

 

  • Package List request parameters using

public class LoginAction extends ActionSupport{
    private List<String> list;

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    @Override
    public String execute() throws Exception {
        return null;
    }

}

The need to provide getter, setter methods.

 

Form field name attribute specifies the List object:

    name:<input type="text" name="list"><br />
    age:<input type="text" name="list"><br />

The request is stored in a List field, the first field is stored as the first element of the List, the second field is stored as second element ...... and so on.

 

You can specify the storage location:

    name:<input type="text" name="list"><br />
    age:<input type="text" name="list[3]"><br />
    tel:<input type="tel" name="list"><br />
    

[Value field name, tel value field, null, age field]

Subscripts specified, in the specified location on the storage; not at the specified index, from front to back, stored in the idle position (null).

 

 

  • Map request parameters using encapsulation

public class LoginAction extends ActionSupport{
    private Map<String,String> map;

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    @Override
    public String execute() throws Exception {
        return null;
    }

}

The need to provide getter, setter methods.

 

Form fields:

    name:<input type="text" name="map['name']"><br />
    age:<input type="text" name="map['age']"><br />

Map is used to store key-value pairs field, so the need to specify the key. Map Object [ 'key'].

 

 

 

 

Servlet objects are native to the Servlet by using Struts2 Action to operate in a manner to make use of the data.

But the operation is still Servlet Cookie objects native convenience.

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11461506.html