The difference between ActionContext and ServletActionContext and getting request, session and other objects

1. ActionContext

    It is the context of Action execution. The context of Action can be regarded as a container, which encapsulates Request, Session, Application, etc. The Request, Session, and Application are of type Map, and the encapsulation inside is Key-value pairs, so this shows that struts2 does not deal with the underlying servlet Api, so many web-related objects are encapsulated, so that the Action can be decoupled from the web layer.

Use ActionContext to get Request, Session, and Application of Map type.

example:

    Get request:

     Map request = ActionContext.getContext().get("request");

    Encapsulate data in request

    request.put("name", value);

    You can use request.getAttribute("name") in the foreground;

   

    get session

    Map session = ActionContext.getContext().getSession();

    Encapsulate data into session

    session.put("name", value);

    Use sessionScope.getAttribute("name"); on the foreground page to get the value encapsulated in the session.

There is a difference between getting the session and the request . The get("reqeust") is used to get the request, and the getSession() is used to get the session.

 

You can also directly operate the request (HttpServletRequest) and response (HttpServletResponse) of Java Servlet Http, which is a bit different from the above example , pay attention to the difference .

ActionContext ctx = ActionContext.getContext();       
      
  HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);

The usage is the same as the request and response usage in Servlet

 

二、ServletActionContext

It inherits ActionContext, so ServletActionContext can also get HttpServetRequest, HttpServletResponse, and it also provides the function of directly accessing Servlet-related objects . The objects it can obtain are:

(1)javax.servlet.http.HttpServletRequest : HTTPservlet请求对象

(2)javax.servlet.http.HttpServletResponse : 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 get HttpRequest, HttpResponse

example

  HttpServletRequest request = ServletActionContext.getRequest();

  HttpServletResponse response = ServletActionContext.getResponse();

Then you can use the request.setAttribute("name", value) method.

 

Summary: It is not difficult to see that there are still many common functions between the two, so we still choose according to our own needs. If we can use the ActionContext object to meet the requirements, try to use the ActionContext to avoid letting us directly access the Servlet object . In addition, do not call methods through ActionContext when the Action has not been instantiated, because the Action instance is created before the ActionContext instance, and some values ​​in the ActionContext have not been set, and will return null.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325552794&siteId=291194637