struts2 获取request、session的方法
一,获取方式:
从Servlet容器中获取的方法
与Servlet容器无关的获取方法
IoC方式
非Ioc方式
二,获取方法
2.1 与servlet无关:
2.1.1 非IoC模式
- ActionContext context ;//该方法还可以取得值栈(getValueStack()),使用push(obj)往值栈中放数据
- Map request ;
- Map session ;
- Map application ;
- //以下初始化方法要放在方法中
- context = ActionContext.getContext();
- request = (Map)context.get("request");//获取request
- session = context.getSession();//获取session
- application = context.getApplication();//获取application
- request.put("req", "request属性");
- session.put("ses", "session属性");
- application.put("app", "application属性");
- <!--jsp中取值-->
- request属性接受:<s:property value="#request.req"/>${requestScope.req}
- session属性接受:<s:property value="#session.ses"/>${sessionScope.ses}
- application属性接受:<s:property value="#application.app"/>${applicationScope.app}
2.1.2 IoC模式
- //实现接口
- public class LoginAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware
- Map request ;
- Map session ;
- Map application ;
- public void setRequest(Map request) {
- this.request = request;
- }
- public void setSession(Map session) {
- this.session = session;
- }
- public void setApplication(Map application) {
- this.application = application;
- }
2.2 与servlet有关
2.2.1 非IoC模式
- HttpServletRequest request;
- HttpSession session ;
- ServletContext application;
- request = ServletActionContext.getRequest();//获取request
- session = request.getSession();
- application = ServletActionContext.getServletContext();//获取application
- request.setAttribute("req", "request属性111");
- session.setAttribute("ses", "session属性111");
- application.setAttribute("app", "application属性111");
2.2.1 IoC模式
- //实现接口
- public class LoginAction extends ActionSupport implements ServletRequestAware,ServletContextAware
- HttpServletRequest request;
- ServletContext application;
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- }
- public void setServletContext(ServletContext application) {
- this.application = application;
- }