struts2中常用Result类型的用法,struts2result用法,一、dispatcher
分享于 点击 3078 次 点评:240
struts2中常用Result类型的用法,struts2result用法,一、dispatcher
一、dispatcher(1)为缺省的result类型,一般情况下我们在struts.xml会这么写:<result name="success">/main.jsp</result>以上写法使用了两个默认,其完整的写法为:# <result name="success" type="dispatcher"> # <param name="location">/maini.jsp</param> # </result>第一个默认:type="dispatcher";第二个默认:设置的为location参数,location只能是页面,不能是另一个action(可用type="chain"解决)。(2)实现方式从doExecute方法看出,有三个出口(finalLocation为要跳转的地址):pageContext.include(finalLocation);dispatcher.forward(request, response); (dispatcher是根据finalLocation创建的)dispatcher.include(request, response);而我们知道,forward与include都是转发到context内部的资源。二、redirect(1)可以重定向到一个页面,另一个action或一个网址。# <result name="success" type="redirect">aaa.jsp</result> # <result name="success" type="redirect">bbb.action</result> # <result name="success" type="redirect">www.baidu.com</result> (2)实现方式:查看doExecute方法,只有一个出口:response.sendRedirect(finalLocation);sendRedirect是重定向,是重新产生一个HTTP请求到服务器,故重定向后其原来所在的action上下文就不可用了。三、chain(1)主要用于把相关的几个action连接起来,共同完成一个功能。# <action name="step1" class="test.Step1Action"> # <result name="success" type="chain">step2.action</result> # </action> # # <action name="step2" class="test.Step2Action"> # <result name="success">finish.jsp</result> # </action>(2)实现方式:查看execute()方法,主要思想如下:// 根据Action名称finalActionName及要调用的方法finalMethodName来new一个代理对象proxy,并执行之# proxy = actionProxyFactory.createActionProxy(finalNamespace, # finalActionName, finalMethodName, extraContext); # proxy.execute();(3)多个action间数据的传递主要有两种方式:1。由于处于chain中的action属于同一个http请求,共享一个ActionContext,故可以在上下文中获取,在页面上可以直接使用。手动获取的方法如下:# HttpServletRequest request = ServletActionContext.getRequest(); # String s=(String)request.getAttribute("propName"); 2。实现ModelDriven接口在Step1Action中,加入getModel:# public Object getModel() { # return message; # }在Step2Action中,加入setModel:# public void setModel(Object o){ # System.out.println("message is:"+o); # }注意,setModel的调用先于execute()方法后于构造方法。//该片段来自于http://byrx.net
用户点评