我是靠谱客的博主 搞怪母鸡,最近开发中收集的这篇文章主要介绍struts2获取request、session、application的四种方式,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


struts2获取request、session、application的四种方式
//获取map类型的request、session、application
public class LoginAction1 extendsActionSupport {
private Map request;
private Map session;
private Map application;
publicLoginAction1() {
request= (Map)ActionContext.getContext().get("request");
session= ActionContext.getContext().getSession();
application= ActionContext.getContext().getApplication();
}
publicString execute() {
request.put("r1","r1");
session.put("s1","s1");
application.put("a1","a1");
returnSUCCESS;
}
}
//采用接口方式获得map类型的request、session、application
public class LoginAction2 extendsActionSupport implements RequestAware,SessionAware, ApplicationAware {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//DIdependency injection
//IoCinverse of control
publicString execute() {
request.put("r1","r1");
session.put("s1","s1");
application.put("a1","a1");
returnSUCCESS;
}
@Override
publicvoid setRequest(Map<String, Object> request) {
this.request= request;
}
@Override
publicvoid setSession(Map<String, Object> session) {
this.session= session;
}
@Override
publicvoid setApplication(Map<String, Object> application) {
this.application= application;
}
}
//获取Http类型的request、session、application
public class LoginAction3 extendsActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
publicLoginAction3() {
request= ServletActionContext.getRequest();
session= request.getSession();
application= session.getServletContext();
}
publicString execute() {
request.setAttribute("r1","r1");
session.setAttribute("s1","s1");
application.setAttribute("a1","a1");
returnSUCCESS;
}
}
//采用接口方式获取Http类型的request、session、application
public class LoginAction4 extendsActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
publicString execute() {
request.setAttribute("r1","r1");
session.setAttribute("s1","s1");
application.setAttribute("a1","a1");
returnSUCCESS;
}
@Override
publicvoid setServletRequest(HttpServletRequest request) {
this.request= request;
this.session= request.getSession();
this.application= session.getServletContext();
}
}


最后

以上就是搞怪母鸡为你收集整理的struts2获取request、session、application的四种方式的全部内容,希望文章能够帮你解决struts2获取request、session、application的四种方式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部