struts2的action中获取request、session、application的四种方式

JSP 做棵大树 来源:博客园 7年前 (2018-04-10) 2245次浏览 0个评论

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<a href="https://beatree.cn/tag/struts" title="查看更多关于 struts 的文章" target="_blank">struts</a>2.x<a href="https://beatree.cn/tag/struts" title="查看更多关于 struts 的文章" target="_blank">struts</a>(HttpServletRequest request) {


this.request= request;

this.session= request.getSession();

this.application= session.getServletContext();

}

}

 


做棵大树 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明struts2 的 action 中获取 request、session、application 的四种方式
喜欢 (2)
[欢迎投币]
分享 (0)
关于作者:
一个整天无所事事的,有时候忽然热血的孩子
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
Ads Blocker Image Powered by Code Help Pro

靓仔,把广告拦截插件关了吧~ 给孩子两毛钱广告费吧~

我们检测到您使用了广告屏蔽插件,建议您把 广告屏蔽插件关闭 或者 添加本站为白名单 即可

个人网站运营不易,感谢体谅啊~

打赏博主会让我更加开心啦~(。・∀・)ノ

Powered By
100% Free SEO Tools - Tool Kits PRO