本文主要是介绍【Struts2学习笔记(8)】访问或添加request/session/application属性获取HttpServletRequest / HttpSession / ServletContex,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
(1)访问或添加request/session/application属性
public String scope() throws Exception{ActionContext ctx = ActionContext.getContext();ctx.getApplication().put("app", "应用范围");//往ServletContext里放入appctx.getSession().put("ses", "session范围");//往session里放入sesctx.put("req", "request范围");//往request里放入reqreturn "scope";
}
JSP:<body>${applicationScope.app} <br>${sessionScope.ses}<br>${requestScope.req}<br></body>
(2)获取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;}
}
这篇关于【Struts2学习笔记(8)】访问或添加request/session/application属性获取HttpServletRequest / HttpSession / ServletContex的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!