Access web elements in sessionapplicationStruts2

6923 단어 application
This article is a post about sessionapplication
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 Control
First, 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------------ ----------------------

좋은 웹페이지 즐겨찾기