传智Struts2笔记(10)获取request/session/application属性
访问或添加request/session/application属性
public String scope() throws Exception{ ActionContext ctx = ActionContext.getContext(); ctx.getApplication().put("app", "应用范围");//往ServletContext里放入app ctx.getSession().put("ses", "session范围");//往session里放入ses ctx.put("req", "request范围");//往request里放入req return "scope"; }
JSP:
<body>
${applicationScope.app}<br>
${sessionScope.ses}<br>
${requestScope.req}<br>
</body>
获取HttpServletRequest/HttpSession/ServletContext/HttpServletResponse对象
方法一,通过ServletActionContext.类直接获取:
public String rsa() throws Exception{ HttpServletRequest request = ServletActionContext.getRequest(); ServletContext servletContext = ServletActionContext.getServletContext(); request.getSession() HttpServletResponse response = ServletActionContext.getResponse(); return "scope"; }
方法二,实现指定接口,由struts框架运行时注入:
public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{ private HttpServletRequest request; private ServletContext servletContext; private HttpServletResponse response; public void setServletRequest(HttpServletRequest req) { this.request=req; } public void setServletResponse(HttpServletResponse res) { this.response=res; } public void setServletContext(ServletContext ser) { this.servletContext=ser; } }
相关推荐
歆萌 2020-08-03
houmenghu 2020-11-17
思君夜未眠 2020-08-25
阳光之吻 2020-08-03
mbcsdn 2020-06-05
我心似明月 2020-11-09
oraclemch 2020-11-06
ltd00 2020-09-12
康慧欣 2020-09-10
waveclouds 2020-09-04
蓝色深海 2020-09-15
jincheng 2020-09-01
取个好名字真难 2020-08-06
婷婷小屋 2020-07-28
solarspot 2020-07-28
MLXY 2020-07-26
dxbjfu0 2020-07-26
songbinxu 2020-07-19
83520298 2020-07-06