Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ ActionContext and ServletActionContext Summary     - CentOS 6.5 installation and simple configuration Nginx (Server)

- Asynchronous JavaScript loading (Programming)

- Redis configuration file interpretation (Database)

- C # Future: Tracking null reference (Programming)

- C ++ constant definition (Programming)

- jQuery get value drop-down list and select text (Programming)

- Ubuntu install driver manager Mint Driver Manager and Device Driver Manager (Linux)

- Check with Hello World Docker installation (Server)

- Linux kernel to achieve soft RPS network to receive soft interrupt load balancing to distribute (Linux)

- Linux Tutorial: Open multiple tabs in the GNOME terminal in Ubuntu 15.04 (Linux)

- Linux system security reinforcement system by masquerading method (Linux)

- Go build the locale under Windows (Linux)

- Linux system started to learn: Teaches you install Fedora 22 on VirtualBox (Linux)

- Why did not Oracle privileges can also log in with sysdba (Database)

- Drawing from the Android source code analysis View (Programming)

- Eclipse 3.7.2 can not start solving under Ubuntu 14.04 (Linux)

- Ubuntu Linux installation GAMIT10.6 (Linux)

- RHEL 5.7 Yum configure local source [Errno 2] No such file or directory (Linux)

- File SUID, SGID, Sticky property (Linux)

- Ubuntu users to install Gnome-Pie 0.6.5 (Linux)

 
         
  ActionContext and ServletActionContext Summary
     
  Add Date : 2018-11-21      
         
         
         
  1. ActionContext

In Struts2 development, in addition to the request parameters are automatically set to the Action field, we often need direct access request (Request) or Session (Session) some information in Action, or even require direct JavaServlet Http request (HttpServletRequest) ., the response (HttpServletResponse) operation we need to get the value of the request parameter request "username" in the Action:

ActionContext context = ActionContext.getContext ();
Map params = context.getParameters ();
String username = (String) params.get ( "username");
ActionContext (com.opensymphony.xwork.ActionContext) is executed in the context of Action, the context can be seen as a container (in fact, we have here the container is just a Map), which is stored in the implementation of Action need to use objects. generally, our ActionContext through: ActionContext context = (ActionContext) actionContext.get (); to get a look at us here actionContext create object:

static ThreadLocal actionContext = new ActionContextThreadLocal ();

ActionContextThreadLocal is to achieve an internal ThreadLocal class .ThreadLocal be named "thread-local variable", which use the variable for each thread provides a copy of the value of a variable so that each thread can independently change their copy, and not a copy of conflicts with other threads. thus, we ActionContext in the property only in the corresponding current request thread visible, so as to ensure that it is thread safe.

By ActionContext get HttpSession: Map session = ActionContext.getContext () getSession ();.

2. ServletActionContext

ServletActionContext (. Com.opensymphony.webwork ServletActionContext), this class directly inherited ActionContext we described above, which provides direct access to the object associated with the Servlet functionality, it can be made the object are:

(1) javax.servlet.http.HttpServletRequest: HTTPservlet request object

(2) javax.servlet.http.HttpServletResponse: HTTPservlet corresponding object

(3) javax.servlet.ServletContext: Servlet context information

(4) javax.servlet.ServletConfig: Servlet configuration object

(5) javax.servlet.jsp.PageContext: Http page context

How to get Servlet related objects from ServletActionContext in:

(1) obtain the HttpServletRequest object: HttpServletRequest request = ServletActionContext getRequest ();.

(2) to obtain the HttpSession object: HttpSession session = ServletActionContext getRequest () getSession ();..

3. ServletActionContext and ActionContext Contact

ServletActionContext and ActionContext has some duplicate functions in our Action, which is how to choose what principles we follow are:? If ActionContext to achieve our function, it is best not to use ServletActionContext on, let's try not to direct the Action Servlet to access related objects.

Note: When using ActionContext One thing to note: Do not use ActionContext.getContext in the constructor's in Action (), because some of the values ​​in this time ActionContext may not set, then the value may be obtained by ActionContext null; similarly, HttpServletRequest req = ServletActionContext.getRequest () constructor should also be avoided, and do not directly req as a class variable to their assignment. As for the reason, I think it was mentioned above the static ThreadLocal actionContext = new ActionContextThreadLocal (), from here we can see ActionContext is thread-safe, but ServletActionContext inherited from ActionContext, so ServletActionContext also thread-safe, thread-safety requirements for each thread are independent, so creating req also requires independent, so ServletActionContext.getRequest () this sentence not in the constructor, and do not directly on the class, and should be placed on each specific method body (eg : login (), queryAll (), insert (), etc.), so as to ensure the establishment of an independent req generated each time the object.

4. struts2 obtained request, response, and session

(1) Non-IoC way

Method One: Use org.apache.struts2.ActionContext class, gets the current Action context object through its static methods getContext ().

ActionContext ctx = ActionContext.getContext ();

ctx.put ( "liuwei", "andy"); //request.setAttribute("liuwei "," andy ");
Map session = ctx.getSession (); // session

HttpServletRequest request = ctx.get (org.apache.struts2.StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = ctx.get (org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
Careful friends can find here the session is a Map object, Struts2 in the bottom of the session it has been packaged in a Map type. We can directly manipulate objects on the Map session of the write and read operations, without having to directly manipulate HttpSession object.

Method 2: Use org.apache.struts2.ServletActionContext class

public class UserAction extends ActionSupport {
    
    // Other code fragment
    
    private HttpServletRequest req;
// Private HttpServletRequest req = ServletActionContext.getRequest (); this statement is wrong in this position, this same statement in the constructor method is wrong.

    public String login () {
        req = ServletActionContext.getRequest (); // req obtained must be implemented in specific method
        user = new User ();
        user.setUid (uid);
        user.setPassword (password);
        if (userDAO.isLogin (user)) {
            . Req.getSession () setAttribute ( "user", user);
            return SUCCESS;
        }
        return LOGIN;
    }
    public String queryAll () {
        req = ServletActionContext.getRequest (); // req obtained must be implemented in specific method
        uList = userDAO.queryAll ();
        req.getSession () setAttribute ( "uList", uList).;
        return SUCCESS;
    }
    
    // Other code fragment
}

(2) IoC mode (ie using Struts2 Aware interceptors)

To use IoC way, we must first tell IoC container (Container) want to get the wishes of an object, by implementing the appropriate interface to do so.

public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {

    private HttpServletRequest request;
    private HttpServletResponse response;

    public void setServletRequest (HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse (HttpServletResponse response) {
        this.response = response;
    }

    public String execute () {
        HttpSession session = request.getSession ();
        return SUCCESS;
    }
}
     
         
         
         
  More:      
 
- OpenJDK 7 compiled under Ubuntu 14.04.3 64-bit (Linux)
- Installation of Ubuntu Make under Ubuntu 15.10 (Linux)
- Android to determine whether the device to open WIFI, GPRS data connection (Programming)
- CentOS-based Kickstart automated installation practice (Linux)
- Linux terminal interface font color settings (Linux)
- Ubuntu 14.10 / 14.04 / 12.04 installation GNOME Pie 0.5.6 (Linux)
- Defensive programming in PHP (Programming)
- Installation CD audio file extraction tool Flacon (Linux)
- Python script file directory traversal examples (Programming)
- Upgrading to MySQL 5.7.9 MySQL 5.6.23 (Database)
- Linux more command Detailed (Linux)
- Protect against network attacks using Linux system firewall (Linux)
- Ubuntu users Steam controller does not work solutions (Linux)
- How to implement large-scale distributed Yahoo depth study on the Hadoop cluster (Server)
- Installation and use GAMIT / GLOBK Software (Linux)
- 20 open source / commercial Linux server management control panel (Server)
- The Java way to stop a thread of execution (Programming)
- Use top to monitor the remote server (Server)
- Performance Optimization: Using Ramlog transfer log files to memory (Linux)
- Oracle Enterprise Linux 64-bit install apache-tomcat-7.0.53 step (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.