取得Map 类型的 request,session,application, HttpServletRequest,HttpSession,ServletContext的引用。
分访问 Map 类型和原始类型两种。
1、获取 Map 类型的request,session,application( Map 类型)的第一种方式,他们保存在都ActionContext中:
package com.bjsxt.struts2.user.action; import java.util.Map; import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class LoginAction1 extends ActionSupport{ private static final long serialVersionUID = -7767523825141043349L; private Map request;
private Map session;
private Map application; private User user; 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;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }
jsp页面的访问:
<s:property value="#request.r1"/>
<s:property value="#session.s1"/>
<s:property value="#application.a1"/>
2、获取 Map 类型的request,session,application的第一种方式,一般用这种就行了:
实现了RequestAware,SessionAware,ApplicationAware 这三个接口,这种方式是通过Struts2把这三个对象注入给Action的。
package com.bjsxt.struts2.user.action; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware; import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport; /**
* 只用这种
* @author Administrator
*
*/
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ private static final long serialVersionUID = -7767523825141043349L; private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application; private User user; public String execute(){
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public Map<String, Object> getRequest() {
return request;
} public void setRequest(Map<String, Object> request) {
this.request = request;
} public Map<String, Object> getSession() {
return session;
} public void setSession(Map<String, Object> session) {
this.session = session;
} public Map<String, Object> getApplication() {
return application;
} public void setApplication(Map<String, Object> application) {
this.application = application;
} }
获取的方式跟第一种是一样的。
3、获取 HttpServletRequest,HttpSession,ServletContext (原始类型)的第一种方式:
package com.bjsxt.struts2.user.action; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction3 extends ActionSupport{ private static final long serialVersionUID = -7767523825141043349L; private HttpServletRequest request;
private HttpSession session;
private ServletContext application; public LoginAction3(){
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
} public String execute(){ return SUCCESS;
}
}
取值,要是request、session、application中存在这个属性的话:
<%=request.getAttribute("r1") %>
<%=session.getAttribute("s1") %>
<%=application.getAttribute("a1") %>
4、获取 HttpServletRequest,HttpSession,ServletContext (原始类型)的第二种方式:
package com.bjsxt.struts2.user.action; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; public class LoginAction4 extends ActionSupport implements ServletRequestAware{ private static final long serialVersionUID = -7767523825141043349L; private HttpServletRequest request;
private HttpSession session;
private ServletContext application; public String execute(){
return SUCCESS;
} @Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
} }
取值方式和第三种一样。
补充:
<s:property value="#attr.r1"/> <s:property value="#attr.s1"/> <s:property value="#attr.a1"/>
这种获取方式会去ActionContext中遍历查找,所有的r1、s1、a1的值,一般情况下我们都会知道变量的值精确存放点,不推荐使用这个取值方式。
链接: http://pan.baidu.com/s/1pLk9ZCJ 密码: idbm