Struts2结果类型

来源:互联网 发布:中国程序员人才网 编辑:程序博客网 时间:2024/04/28 07:43

在struts2框架中,当action处理完之后,就应该向用户返回结果信息,该任务被分为两部分:结果类型和结果本身。

结果类型提供了返回给用户信息类型的实现细节。结果类型通常在Struts2中就已预定义好了(见下表),或者是由插件提供,开发人员也可以自定义结果类型。默认配置的结果类型是dispatcher,该结果类型使用JSP来向用户显示结果。当定义了结果类型之后,该结果类型可以在不同的action中重复使用。

Struts2框架提供的结果类型

已配置结果类型名类  名描  述dispatcherorg.apache.struts2.dispatcher.
ServletDispatcherResult默认结果类型,用来呈现JSP页面chaincom.opensymphony.xwork2.
ActionChainResult将action和另外一个action链接起来freemarkerorg.apache.struts2.views.freemarker.
FreemarkerResult呈现Freemarker模板httpheaderorg.apache.struts2.dispatcher.
HttpHeaderResult返回一个已配置好的HTTP头信息响应redirectorg.apache.struts2.dispatcher.
ServletRedirectResult将用户重定向到一个已配置好的URLredirectActionorg.apache.struts2.dispatcher.
ServletActionRedirectResult将用户重定向到一个已定义好的actionstreamorg.apache.struts2.dispatcher.
StreamResult将原始数据作为流传递回浏览器端,
该结果类型对下载的内容和图片非常有用velocityorg.apache.struts2.dispatcher.
VelocityResult呈现Velocity模板xsltorg.apache.struts2.views.xslt.
XSLTResult呈现XML到浏览器,
该XML可以通过XSL模板进行转换plaintextorg.apache.struts2.dispatcher.
PlainTextResult返回普通文本类容

 

简单说明一下result的name属性和type属性:


SUCCESS:Action正确的执行完成,返回相应的视图,success是name属性的默认值。


NONE:表示Action正确的执行完成,但并不返回任何视图。


ERROR:表示Action执行失败,返回到错误处理视图。


INPUT:Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入的界面,一般在应用中,会对这些参数进行验证,如果验证没有通过,将自动返回到该视图。


LOGIN:Action因为用户没有登陆的原因没有正确执行,将返回该登陆视图,要求用户进行登陆验证。


dispatcher:请求转发,底层调用RequestDispatcher的forward()或include()方法,dispatcher是 type属性的默认值,通常用于转向一个JSP。localtion指定JSP的位置,parse如果为false表示location的值不会被当作 OGNL解析,默认为true。


redirect:重定向,新页面无法显示Action中的数据,因为底层调用response.sendRedirect("")方法,无法共享请求范围内的数据,参数与dispatcher用法相同。


redirect-action:重定向到另一个Action,参数与chain用法相同,允许将原Action中的属性指定新名称带入新Action 中,可以在Result标签中添加 <param name=”b”>${a} </param>,这表示原Action中的变量a的值被转给b,下一个Action可以在值栈中使用b来操作,注意如果值是中文,需要做一些编码处理,因为Tomcat默认是不支持URL直接传递中文的!

velocity:使用velocity模板输出结果,location指定模板的位置(*.vm),parse如果为false,location不被OGNL解析,默认为true。
xslt:使用XSLT将结果转换为xml输出,location指定*.xslt文件的位置,parse如果为false,location不被 OGNL解析,默认为true。matchingPattern指定想要的元素模式,excludePattern指定拒绝的元素模式,支持正则表达式,默认为接受所有元素。


httpheader:根据值栈返回自定义的HttpHeader,status指定响应状态(就是指response.sendError(int i)重定向到500等服务器的状态页)。parse如果为false,header的值不会被OGNL解析,headers,加入到header中的值,例如: <param name=”headers.a”>HelloWorld </param>。可以加多个,这些键-值组成HashMap。


freemaker:用freemaker模板引擎呈现视图,location指定模板(*.ftl)的位置,parse如果为false,location的值不会被OGNL解析。contentType指定以何中类型解析,默认为text/html。


chain:将action的带着原来的状态请求转发到新的action,两个action共享一个ActionContext,actionName指定转向的新的Action的名字。method指定转向哪个方法,

namespace指定新的Action的名称空间,不写表示与原Action在相同的名称空间;skipActions指定一个使用 , 连接的Action的name组成的集合,一般不建议使用这种类型的结果。


stream:直接向响应中发送原始数据,通常在用户下载时使用,contentType指定流的类型,默认为 text/plain,contentLength以byte计算流的长度,contentDisposition指定文件的位置,通常为 filename=”文件的位置”,input指定InputStream的名字,例如:imageStream,bufferSize指定缓冲区大小,默认为1024字节。


plaintext:以原始文本显示JSP或者HTML,location指定文件的位置,charSet指定字符集。

dispatcher 结果类型为缺省的result类型,用于返回一个视图资源(如:jsp)Xml代码 :<result name="success">/main.jsp</result> <result name="success">/main.jsp</result>以上写法使用了两个默认,其完整的写法为:   <result name="success" type="dispatcher">      <param name="location">/maini.jsp</param> </result> location只能是页面,不能是另一个action(可用type="chain"解决)。redirect 结果类型用于重定向到一个页面,另一个action或一个网址。Xml代码:<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>redirect-action 结果类型使用ActionMapperFactory提供的ActionMapper来重定向请求到另外一个actionXml代码:<result name="err" type="redirect-action">     <param name="actionName">重定向的Action名</param>      <param name="namespace">重定向Action所在的名字空间</param> </result> redirect和redirect-action两种结果类型在使用上其实并没有什么区别,只是写法不同而已。chain 用于把相关的几个action连接起来,共同完成一个功能。Xml代码:<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> 处于chain中的action属于同一个http请求,共享一个ActionContext   plaintextj 结果类型用于直接在页面上显示源代码Xml代码:<result name="err" type="plaintext">     <param name="location">具体的位置</param>     <param name="charSet">字符规范(如GBK)</param> </result>