Java Web之Struts2访问Servlet API

方法一:直接获取 Map类型

Map request = (Map)Actioncontext.getContext().get("request");
Map session = ActionContext.getContext.getSession();
Map application = ActionContext.getContext.getApplication();

比如使用:request.put("name",name);
可以在Jsp页面获取 <s:property value="#request.name"/> 
说明:<s:property/>为struts2的标签。

方法二:实现接口,依赖struts2注入 Map 类型

public class LoginAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
     private Map<String, object> request;
     private Map<String, object> session;
     private Map<String, object> application;
     @override
     public void setRequest(Map<String,object> request){
            this.request=request;
     } 
     @override
      public void setSession(Map<String,object> session){
            this.session=session;
     } 
     @override
      public void setApplication(Map<String,object> application){
            this.application=application;
     } 
}

方法三:获取httpServlet类型的

private HttpServletRequest request = ServletActionContext.getRequest();
private HttpSession session = request.getSession();
private ServletContext application = session.getServletContext();
比如使用request :request.setAttribute("name",name);

方法四:获取httpServlet类型的,实现ServletRequestAware 接口

public class LoginAction extends ActionSupport implements ServletRequestAware{
     private HttpServletRequest request;
     private HttpSession session;
     private ServletContext application;
     public void setServletRequest(HttpServletRequest ruequst){
          this.request = request;
          this.session = request.getSession();
          this.application = session.getServletContext();
     }
}
上一篇:iOS开发之AFNetWorking初次使用会报错的坑


下一篇:Java历程-初学篇 Day02变量,数据类型和运算符