How to obtain JSP page parameters for actions in struts2

Source: Internet
Author: User

1. actioncontext

In struts2 development, in addition to automatically setting request parameters to the action field, we often need to directly obtain the request or session in the action.
(Session) information, or even directly to the assumervlet
HTTP Request (httpservletrequest), response (httpservletresponse) operation.
We need to obtain the value of the request parameter "username" in the action:

Actioncontext context = actioncontext. getcontext ();
Map Params = context. getparameters ();
String username = (string) Params. Get ("username ");
Actioncontext (COM. opensymphony. xwork. actioncontext) is the context of Action execution.
As a container (in fact, the container here is just a map), It stores the objects required for action execution. Generally,
Our actioncontext is implemented through: 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 internal class implementing threadlocal. threadlocal can be named as "local thread change
", It provides a copy of the variable value for every thread that uses this variable, so that each thread can change its own copy independently without conflict with the copies of other threads. in this way, we
The attributes in actioncontext are only visible in the corresponding current request thread to ensure thread safety.

Httpsession: Map session = actioncontext. getcontext (). getsession ();

2. servletactioncontext

Servletactioncontext (COM. opensymphony. webwork.
Servletactioncontext), this class directly inherits the actioncontext we introduced above, which provides access directly to servlet-related objects
Function, which can obtain the following objects:

(1) javax. servlet. http. httpservletrequest: httpservlet request object

(2) javax. servlet. http. httpservletresponse: corresponding object of httpservlet

(3) javax. servlet. servletcontext: servlet context information

(4) javax. servlet. servletconfig: servlet configuration object

(5) javax. servlet. jsp. pagecontext: HTTP Page Context

How to obtain servlet-related objects from servletactioncontext:

<1> get the httpservletrequest object: httpservletrequest request = servletactioncontext. getrequest ();

<2> obtain the httpsession object: httpsession session = servletactioncontext. getrequest (). getsession ();

3. Contact servletactioncontext and actioncontext

Servletactioncontext and actioncontext have some repeated functions. In our actions, how should we choose? What we follow
The principle is: If actioncontext can implement our functions, it is best not to use servletactioncontext, so that our actions should not
Access servlet objects directly.

Note: When Using actioncontext, note the following:
Do not use actioncontext. getcontext () in the Action constructor, because some values in actioncontext may not
The value obtained through actioncontext may be null. Similarly, httpservletrequest Req =
Servletactioncontext. getrequest () should not be placed in the constructor, or be directly assigned a value to req as a class variable. For the reason, I think
Static threadlocal actioncontext = new
Actioncontextthreadlocal (), from which we can see that actioncontext is thread-safe, while
Servletactioncontext inherits from actioncontext, so servletactioncontext is also thread-safe, thread security requirements
Threads are independent, so the creation of req must also be independent. Therefore, the sentence servletactioncontext. getrequest () should not be placed in the constructor.
Instead of directly placing them in the class, they should be placed in each specific method body (eg: Login (), queryall (), insert (), etc ), in this way, the object is generated every time.
A req is created independently.

4. GET request, response, and session in struts2

(1) Non-IOC Mode

Method 1: Use the org. Apache. struts2.actioncontext class to obtain the context object of the current action through its static method 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 that the session here is a map object, and the underlying sessions in struts2 are encapsulated into the map type. we can directly operate on this map object to write and read sessions, instead of directly operating on the httpsession object.

Method 2: Use the org. Apache. struts2.servletactioncontext class

Public class useraction extends actionsupport {

// Other code snippets

Private httpservletrequest req;
// Private httpservletrequest Req = servletactioncontext. getrequest (); this statement is incorrect at this position. It is also wrong to put this statement in the constructor.

Public String login (){
Req = servletactioncontext. getrequest (); // obtain req must be implemented in the 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 (); // obtain req must be implemented in the specific method
Ulist = userdao. queryall ();
Req. getsession (). setattribute ("ulist", ulist );
Return success;
}

// Other code snippets
}
(2) IOC (that is, struts2 aware interceptor)

To use the IOC method, we must first tell the IOC container (container) The willingness to obtain an object and implement the corresponding interface to achieve this.

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;
}
}

 

Address: http://hi.baidu.com/346667002/blog/item/e58edef0d599fcc97831aa18.html

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.