The actioncontext of struts is detailed

Source: Internet
Author: User
Tags http request

Form url:http://apps.hi.baidu.com/share/detail/16057446

Actioncontext (Action context)

Actioncontext Introduction

Through the above user registration example Learning , we know that xwork and Web -agnostic, our action does not have to rely on any web container, Do not associate with those Javaservlet complex requests (request), responses (Response). For the request parameter (Param), you can use the Interceptor framework to automatically invoke some get () and set () methods into the corresponding action fields. However, simply obtaining the value of the request parameter will fully satisfy our functional requirements. No, in Web application development, in addition to automatically setting the request parameters to the Action field, we often need to get some information about the request or session in the action directly, or even directly to the Javaservlet HTTP request (HttpServletRequest), response (HttpServletResponse) operation.

With these questions, let's take a look at one of the following functional requirements:

We need to get the value of the request parameter "username" in action:

Actioncontext context = Actioncontext.getcontext ();

Map params = Context.getparameters ();

String username = (string) params.get ("username");

In order to achieve this function, we have used three steps:

1, to obtain our current Actioncontext object Context,actioncontext is what winter winter.

2, from the context object to get all of our request parameters, obtained is a map object params.

3. We can get the value of the request parameter "username" from our map object params.

Actioncontext (Com.opensymphony.xwork.ActionContext) is the context in which the action executes, and the context can be thought of as a container (in fact, our container is a map), and it holds the pair that the action needs to be executed. Like, for example: When using WebWork, our context has the requested parameters (Parameter), Session, Servlet context (ServletContext), localization (locale) information, and so on.

It is thread safe to create a new actioncontext,actioncontext each time the action is executed, meaning that the properties in the same line thread actioncontext are unique so that my action can be used in multiple threads.

We can get the current Actioncontext object by Actioncontext static method: Actioncontext.getcontext (), let's take a look at this code:

public static Actioncontext GetContext () {

Actioncontext context = (Actioncontext) actioncontext.get ();


if (context = = null) {

Ognlvaluestack vs = new Ognlvaluestack ();

context = new Actioncontext (Vs.getcontext ());

SetContext (context);

}


return context;

}

In general, our Actioncontext are obtained by: actioncontext context = (Actioncontext) actioncontext.get (); Let's take a look at the creation of the Actioncontext object here: static ThreadLocal Actioncontext = new actioncontextthreadlocal (); Actioncontextthreadlocal is an inner class that implements Threadlocal. Threadlocal can be named "Thread-local variable", which provides a copy of the value of a variable for each thread that uses the variable, so that each thread can independently change its copy without conflicting with the other thread's copy. In this way, the properties in our Actioncontext are only visible in the corresponding current request thread, ensuring that it is thread-safe.

Let's see how we can get our httpsession through Actioncontext:

Map session = Actioncontext.getcontext (). GetSession ();

It turns out that the session we got was a map type object, which is why. It turns out that our WebWork framework has repackaged many objects related to the web, such as repackaging httpsession objects into a map object for use by our action instead of dealing directly with the underlying httpsession. is also the framework of packaging, so that our actoion can completely and the Web layer decoupling.

If our action needs to be directly related to Javaservlet's httpsession, HttpServletRequest and other objects, what should we do with it? Take a look at the servletactioncontext below.

Servletactioncontext

Servletactioncontext (Com.opensymphony.webwork servletactioncontext), this class directly inherits the Actioncontext we described above, It provides direct access to Javaservlet-related objects, and it can get objects that are:

1. Javax.servlet.http.HttpServletRequest:HTTPservlet Request Object

2. Javax.servlet.http.httpservletresponse;:httpservlet corresponding Object

3. Javax.servlet.ServletContext:Servlet Contextual Information

4. Javax.servlet.ServletConfig:Servlet Configuration Object

5. Javax.servlet.jsp.PageContext:Http Page Context

In addition to providing these object access, Servletactioncontext also inherits many of the features of its parent class Actioncontex, such as access to ognlvaluestack, action names, and so on.

Let's take a look at a few simple examples of how to get Javaservlet related objects from Servletactioncontext:

1. Get HttpServletRequest object:

HttpServletRequest request = Servletactioncontext. Getrequest ();

2. Get HttpSession object:

HttpSession session = Servletactioncontext. Getrequest (). GetSession ();

Servletactioncontext and Actioncontext have some repetitive functions, and in our action, how to choose. We follow the principle that if actioncontext can achieve our function, it is best not to use Servletactioncontext, let our action try not to direct access to javaservlet related objects. One thing to note when using Actioncontext: Do not use Actioncontext.getcontext () in the action constructor, because some values in Actioncontext may not be set at this time, The value obtained by Actioncontext may be null.

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.