本文是一篇关于sessionapplication的帖子
取得Map类型request,session,application,实在类型 HttpServletRequest, HttpSession, ServletContext的引用:
前三者:依赖于容器
前三者:IOC (只用这种)
后三者:依赖于容器
后三者:IOC
一、 方法一:ActionContext方法
一般在Action类的构造方法、或execute()方法中获得。
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;
}
}
然后在Jsp页面中获得相关web元素。
<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>
注:因为request、session、application对象Struts2将在放入到Action Context中,
因此需要应用#key来访问对象们。
后面的是java脚本代码的访问方法。
一、 方法二:Ioc(控制反转)—推荐应用
让Action类实现RequestAware、SessionAware、ApplicationAware接口,然后重写他们的set方法(setRequest、setSession、setApplication),通过依赖注入、控制反转(本来自己控制,现在由别人来控制值。)
记不清有多少个夜晚,在我翻阅纸张的指间滑落;记不清有多少支蜡烛,在我的凝视中化为灰烬。逝者如斯,我时时刻刻会听见自己对生命承诺的余音,感到岁月的流转在渐渐稀释我的年少无知,我愿自己是一只上足了发条的时钟,在昼夜不停的流转中留下自己充实的每一刻。
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;
}
}
在视图(JSP)页面中获得相关对象,同方法一。
一、 方法三:获得原类型
获得是的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;
}
}
一、 方法四:获得原类型-控制反转
首先需要Action实现org.apache.struts2.interceptor.ServletRequestAware接口,然后重写setServletRequest()方法,获得HttpServletRequest对象,再通过HttpServletRequest对象取昨HttpSession和ServletContext对象。
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();
}
}
文章结束给大家分享下程序员的一些笑话语录: 系统程序员
1、头皮经常发麻,在看见一个蓝色屏幕的时候比较明显,在屏幕上什幺都看不见的时候尤其明显;
2、乘电梯的时候总担心死机,并且在墙上找reset键;
3、指甲特别长,因为按F7到F12比较省力;
4、只要手里有东西,就不停地按,以为是Alt-F、S;
5、机箱从来不上盖子,以便判断硬盘是否在转;
6、经常莫名其妙地跟踪别人,手里不停按F10;
7、所有的接口都插上了硬盘,因此觉得26个字母不够;
8、一有空就念叨“下辈子不做程序员了”;
9、总是觉得9号以后是a号;
10、不怕病毒,但是很害怕自己的程序;
---------------------------------
原创文章 By
session和application
---------------------------------