【struts2】访问web元素三种方法

发布时间:2016-12-9 17:50:51 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"【struts2】访问web元素三种方法",主要涉及到【struts2】访问web元素三种方法方面的内容,对于【struts2】访问web元素三种方法感兴趣的同学可以参考一下。

访问web元素 map类型的 request、session、application;真实类型的 httpservletrequest、httpsessiont、servletcontext 1、使用ActionContext访问web元素 private Map request ; private Map session ; private Map application ; public IndexAction(){ request = (Map)ActionContext.getContext().get("request"); session = ActionContext.getContext().getSession(); application = ActionContext.getContext().getApplication(); } public String add() { request.put("r1","request"); session.put("s1","session"); application.put("a1","application"); return SUCCESS; }jsp页面访问 <s:property value="#request.r1" /><br /> <s:property value="#session.s1" /><br /> <s:property value="#application.a1" /> <s:debug></s:debug> 2、依赖于容器,IoC、DI public class IndexAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ private Map<String,Object> request ; private Map<String,Object> session ; private Map<String,Object> application ; public void setRequest(Map<String,Object> request){ this.request = request ; } public void setSession(Map<String,Object> session){ this.session = session ; } public void setApplication(Map<String,Object> application){ this.application = application ; } public String add() { request.put("r1","awarerequest"); session.put("s1","awaresession"); application.put("a1","awareapplication"); return SUCCESS; } } 3、通过ServletActionContext获取原始的web元素类型 public class IndexAction extends ActionSupport { private HttpServletRequest request ; private HttpSession session ; private ServletContext application ; public IndexAction(){ request = ServletActionContext.getRequest(); session = request.getSession(); application = session.getServletContext(); } public String add() { request.setAttribute("r1", "httprequest"); session.setAttribute("s1","session"); application.setAttribute("a1","application"); return SUCCESS; } }

上一篇:vector迭代器的begin()和end()
下一篇:ul和li实现分两列(多列)布局显示

相关文章

相关评论