Access web elements in sessionapplicationStruts2
6923 단어 application
Get the reference of Map type request, session, application, real type HttpServletRequest, HttpSession, ServletContext:
The first three: depend on the container
The first three: IOC (only this one)
The last three: depend on the container
The last three: IOC
1. Method 1: ActionContext method
Generally, it is obtained in the constructor of the Action class, or the execute() method.
public class LoginAction1 extends ActionSupport {
private Map request;
private Map session;
private Map application;
public LoginAction1() {
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
}
Then get the relevant web elements in the Jsp page.<body>
User Login Success!
<br />
<s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
<s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
<s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />
<s:property value="#attr.a1"/><br />
<s:property value="#attr.s1"/><br />
<s:property value="#attr.r1"/><br />
<s:debug></s:debug>
<br />
</body>
Note: Because the request, session, and application objects Struts2 will be put into the Action Context,So need to apply #key to access objects.
The latter is the access method of the java script code.
1. Method 2: Ioc (Inversion of Control) - Recommended Application
Let the Action class implement the RequestAware, SessionAware, and ApplicationAware interfaces, and then rewrite their set methods (setRequest, setSession, setApplication) through dependency injection and inversion of control (originally controlled by themselves, and now others control the value.)
Every day
I can't remember how many nights it slipped between my fingers flipping through the papers; I can't remember how many candles turned to ashes under my gaze. The deceased is like this, I always hear the remnants of my commitment to life, and feel that the passage of time is gradually diluting my youth and ignorance. I wish I was a clock that was wound up, running around day and night. Leave every moment that you have full of yourself.
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//DI dependency injection
//IoC inverse of control
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
Get related objects in the view (JSP) page, same as method 1.1. Method 3: Get the original type
Get yes HttpServletRequest/HttpSession/ServletContext
public class LoginAction3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction3() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
}
1. Method 4: Get the original type - Inversion of ControlFirst, Action needs to implement the org.apache.struts2.interceptor.ServletRequestAware interface, then rewrite the setServletRequest() method to obtain the HttpServletRequest object, and then obtain the HttpSession and ServletContext objects through the HttpServletRequest object.
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction4 extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
At the end of the article, I will share with you some jokes from programmers: System programmers 1. The scalp is often numb, which is more obvious when seeing a blue screen, especially when nothing is visible on the screen; 2. Multiplication When the elevator is always worried about the crash, and find the reset button on the wall; 3. The nails are very long, because it is more labor-saving to press F7 to F12; ; 5. The case is never covered, so as to judge whether the hard disk is rotating; 6. Often inexplicably follow others, and keep pressing F10 in the hand; 7. All ports are plugged into the hard disk, so I feel that 26 letters are not enough; 8. , Whenever I have time, I will say "I won't be a programmer in my next life"; 9. I always feel that after the 9th, I will be a number a; 10. I am not afraid of viruses, but I am very afraid of my own programs;--------------------------------- Original article By session and application------------ ----------------------
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.