Access Request, Session and Application in Struts2
one,
We can obtain Map type objects such as Request through the ActionContext object for assignment and parameter transfer. The code reference is as follows:
1: public String execute(){
2:
3: ((Map)ActionContext.getContext().get("request")).put("r1", "r1");//Request Map
4: ActionContext.getContext().getSession().put("s1", "s1");// Session Map
5: ActionContext.getContext().getApplication().put("a1", "a1");//Application Map
6: return SUCCESS;
7: }
By defining Action in the above way, parameters can be passed. For this, we have two ways to obtain parameters on the front JSP page:
One is to access parameters through tags:
1:
value=
"#request.r1"/>
2:
value=
"#session.s1"/>
3:
value=
"#application.a1"/>
This access method and adding a # in front of the value parameter, because these are the keys in the Stack Context.
Another way to access parameters is that we are familiar with:
1: <%=request.getAttribute("r1") %>
2: <%=session.getAttribute("s1") %>
3: <%=application.getAttribute("a1") %>
Maybe some people don't understand, Map-type Request, Session, and Application can be accessed in this way? ! Yes, Struts2 did it, and it should have done some conversions internally, converting Map into HttpServletRequest, HttpSession and ServletContext.
This method is dependent on the Struts2 environment, because the ActionContext class is used.
two,
This method is achieved through Dependency Injection (DI, Dependency Injection), or Inversion of Control (IoC, Inverse of Control). This method is most commonly used.
The principle is to define Action to implement RequestAware, SessionAware, and ApplicationAware interfaces, implement the setXxxxx( Map xxx ) method defined in it, and then wait for Struts2 Context to inject request, session, application, etc. into our defined Action. (The understanding of control inversion can be considered that the variables of request, session, and application are controlled by Action itself. After implementing these Aware interfaces, the control is transferred to Struts2 Context.)
The Action code is as follows, and the other codes are as above:
1: package com.cdp.struts2;
2:
3: import java.util.Map;
4:
5: import org.apache.struts2.interceptor.ApplicationAware;
6: import org.apache.struts2.interceptor.RequestAware;
7: import org.apache.struts2.interceptor.SessionAware;
8:
9: import com.opensymphony.xwork2.ActionSupport;
10:
11: public class Action2 extends ActionSupport implements RequestAware,
12: SessionAware, ApplicationAware {
13:
14: private Map
request;
15: private Map
session;
16: private Map
application;
17:
18: public String execute() {
19:
20: request.put("r1", "r1");
21: session.put("s1", "s1");
22: application.put("a1", "a1");
23: return SUCCESS;
24: }
25:
26: public void setRequest(Map
request) {
27: this.request = request;
28:
29: }
30:
31: public void setSession(Map
session) {
32: this.session = session;
33:
34: }
35:
36: public void setApplication(Map
application) {
37: this.application = application;
38:
39: }
40:
41: }
It should be noted that this method is the most commonly used.
three,
The third way is to directly create the real request , session , and application objects that are not of type Map, as follows:
1: package com.cdp.struts2;
2:
3: import javax.faces.application.Application;
4: import javax.servlet.ServletContext;
5: import javax.servlet.http.HttpServletRequest;
6: import javax.servlet.http.HttpSession;
7:
8: import org.apache.struts2.ServletActionContext;
9:
10: import com.opensymphony.xwork2.ActionSupport;
11:
12: public class Action3 extends ActionSupport{
13:
14: private HttpServletRequest request;
15: private HttpSession session;
16: private ServletContext application;
17:
18: public String execute(){
19: request=ServletActionContext.getRequest();
20: session=request.getSession();
21: application=session.getServletContext();
22: request.setAttribute("r1", "r1");
23: session.setAttribute("s1", "s1");
24: application.setAttribute("a1", "a1");
25: return SUCCESS;
26: }
27:
28: }
Four,
The last one is also implemented by dependency injection. Action implements the ServletRequestAware interface, so that the session can be obtained through the request, and the application can be obtained through the session:
1: package com.cdp.struts2;
2:
3: import javax.faces.application.Application;
4: import javax.servlet.ServletContext;
5: import javax.servlet.http.HttpServletRequest;
6: import javax.servlet.http.HttpSession;
7:
8: import org.apache.struts2.ServletActionContext;
9: import org.apache.struts2.interceptor.ServletRequestAware;
10:
11: import com.opensymphony.xwork2.ActionSupport;
12:
13: public class Action4 extends ActionSupport implements ServletRequestAware{
14:
15: private HttpServletRequest request;
16: private HttpSession session;
17: private ServletContext application;
18:
19: public String execute(){
20: request.setAttribute("r1", "r1");
21: session.setAttribute("s1", "s1");
22: application.setAttribute("a1", "a1");
23: return SUCCESS;
24: }
25:
26: public void setServletRequest(HttpServletRequest request) {
27: this.request=request;
28: this.session=request.getSession();
29: this.application=session.getServletContext();
30: }
31:
32: }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JWT로 세션을 관리하지 마십시오.세상에는 JWT(JOSE/JWS/JWE)로 세션 관리를 해서는 안 된다는 기사가 2017년부터 산 정도 있는데 왠지 JWT에서 세션 관리를 하려고 하는 사람이 있다. 번역 기사이거나 암호의 설명이 너무 길거나 해서,...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.