[STRUTS2] Accessing Request,session and Application objects (GO)

Source: Internet
Author: User

How to access the Servlet API decoupling

Structs2 httpservletrequest,httpsession, and ServletContext were encapsulated, three map objects were constructed to replace these three objects, in action, Use the Httpservletrequest,httpsession,servletcontext Map object directly to save and read data.

To get these three map objects, you can use the Com.opensymphony.xwork2.ActionContext class.

Actioncontext is the context in which the action executes, and the set of objects needed to execute the action, including Parameters,request,session,application,locale, is saved in Actioncontext.

The Actioncontext class does not provide a method like Getrequest () to obtain a map object that encapsulates HttpServletRequest. To request a Map object, you need to pass the parameter "request" with the Get () method

[HTML]View Plaincopy
    1. public object get (object key)
    2. Public Map getsession ()
    3. Public Map getapplication ()

Action:

[Java]View Plaincopy
    1. Actioncontext context = Actioncontext.getcontext ();
    2. Map request= (map) context.get ("request");
    3. Map session = Context.getsession ();
    4. MAP application = Context.getapplication ();
    5. Request.put ("greeting", " Hello request");
    6. Session.put ("user", user);
    7. Integer count = (integer) application.get ("counter");
    8. Application.put ("counter", count);


Jsp:

[HTML]View Plaincopy
    1. <h3>${sessionscope.user.username},${requestscope.greeting}. </h3><br />
    2. ${applicationscope.counter}


There is also a way to pass data using the Request object, which can be saved to actioncontext directly using the put () method of the Actioncontext class

[Java]View Plaincopy
    1. Actioncontext.getcontext (). Put ("greeting","Hello");

Then, in the results page, remove the Greeting property from the Request object

[Java]View Plaincopy
    1. $ (requestscope.greeting) or <%=request.getattribute ("greeting")%>

The data stored in the Actioncontext can be obtained from the request object, The secret lies in the Org.apache.struts2.dispatcher.StrutsRequestWrapper class in Struct2, which is the HttpServletRequest wrapper class, which overrides the GetAttribute () method, in which the object is requested to find the attribute, and if not, it is looked up from the actioncontext. This is why the data saved in Actioncontext can be obtained from the request object.


In addition to Actioncontext to get request,session and application objects this way, the action class can implement certain interfaces, allowing Structs2 to inject a request to the action instance at run time. The Session,application object. The corresponding three interfaces and their methods:

[Java]View Plaincopy
    1. Org.apache.struts2.interceptor.RequestAware
    2. Org.apache.struts2.interceptor.SessionAware
    3. Org.apache.struts2.interceptor.ApplicationAware

[Java]View Plaincopy
  1. Public class Loginaction implements action,requestaware,sessionaware,applicationaware{
  2. private MAP request;
  3. Private MAP session;
  4. Private MAP Application;
  5. // ......
  6. @override
  7. Public void Setrequest (Map request) {
  8. This.request=request;
  9. }
  10. @override
  11. Public void Setsession (Map session) {
  12. This.session=session;
  13. }
  14. @override
  15. Public void Setapplication (Map application) {
  16. This.application=application;
  17. }
  18. }

How access is coupled to the Servlet API

To get the HttpServletRequest and ServletContext objects directly, you can use the Org.apache.struts2.ServletActionContext class, where two static methods are defined

[Java]View Plaincopy
    1. Public static Httpserlvetrequest getrequest ()
    2. Public static HttpServletResponse GetResponse ()
    3. Pubolic static ServletContext getservletcontext ()

The HttpSession object can be obtained through the HttpServletRequest object.

You can also call the Get () method of the Actioncontext object to pass Servletactioncontext.http_request and servletactioncontext.servlet_ The context key value to get the HttpServletRequest and ServletContext objects, as follows:

[Java]View Plaincopy
    1. Actioncontext.getcontext (). get (Servletactioncontext.http_request);
    2. Actioncontext.getcontext (). get (Servletactioncontext.http_response);
    3. Actioncontext.getcontext (). get (Servletactioncontext.servlet_context);

[Java]View Plaincopy
    1. Httpservletresquest request = Servletactioncontext.getrequest ();
    2. HttpSession session = Request.getsession ();
    3. ServletContext context = Servletactioncontext.getservletcontext ();
    4. Saving data in Application
    5. Integer count = (integer) context.getattribute ("counter");

In addition to using Servletactioncontext to get HttpServletRequest objects and ServletContext objects this way, The action class can also implement the Servletrequestaware and Servletcontextaware interfaces, which are injected by Struts2 into the action instance httpservletrequest and ServletContext objects

The Serveletrequestaware interface and the Servletcontextaware interface are not in the same package, The former is in the Org.apache.structs2.interceptor package, the latter in the Org.apache.struts2.util package.

[Java]View Plaincopy
    1. Public class Loginaction implements action,servletrequestaware,servletcontextaware{
    2. private HttpServletRequest request;
    3. Private ServletContext context;
    4. //......
    5. @override
    6. Public void Setservletrequest (...) {  
    7. //...
    8. }
    9. @override
    10. Public void Setservletcontext (...) {  
    11. //...
    12. }
    13. }

[STRUTS2] Accessing Request,session and Application objects (GO)

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.