和Webwork1.x不同,Webwork2的Action执行完后,其Result对应一个 Result Type,而这个Result Type完全可以根据具体应用或环境自己进行 定义,只需实现com.opensymphony.xwork.Result接口。Result Type使得Action的执行结果表现形式可以灵活多变!下面这会举例说明,这里先看看Webwork2提供的几种Result Type的定义,该定义在webwork-default.xml中,xwork.xml文件包含了该文件,自定义的Result Type可以直接写在 xwork.xml中: <result-types> <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/> <result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/> <result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/> <result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/> <result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/> <result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/> </result-types> 其大多都有location和parse两个参数,location指明action执行后接着去哪里,parse指明是否对location进行OGNL表达式解析。 1) dispatcher action执行完后,请求会导向对应的View,Webwork2幕后其实是用RequestDispatcher来处理的,所以原 Request/Response对象会接着传递,原Request中的Atrributes不会丢失,这点与下面的redirect是不同的。 2) redirect 对上次的响应将重定向到指定的位置,redirect是重新产生一个新的Request,原来Request保存的东西将不再有效,比如不能通过requet.getAtrribute 取得原来set的对象,也不能取得action的实例,errors,field errors等,因为Action是建立在Single-thread model上的。 3) chain action链,特殊的View调用方式,一个action执行完接着调用另一个action。有个必须的参数actionName,指明紧接着调用的另一action对象。如: <result name="success" type="chain"> <param name="actionName">bar</param> <param name="namespace">/foo</param> </result> 执行后接着调用下面的action: <action name="bar" class="myPackage.barAction"> ... </action> 4) velocity 5) freemarker 6) jasperreports 7) xslt 以上都是用不同技术的产生不同的View。 下面我举个自定义Result Type的示例,假如我有个Action testSendmail,根据处理结果将给指用户发送一份email。自定义一个Result Type,实现Result接口。 com.mycompany.webwork.example.SendmailResult 有三个必须参数:from ,to, subject,一个可选参数 body。 在xwork.xml中定义如下: <result-types> <result-type name="sendmail" class="com.mycompany.webwork.example.SendmailResult"/> </result-types> action定义: <action name="testSendmail" class="com.mycompany.webwork.example.TestSendMailAction"> <result name="success" type="sendmail"> <param name="from">root@sina.com</param> <param name="to">user@sina.com</param> <param name="subject">hello,webwork!</param> </result> <result name="error" type="dispatcher"> <param name="location">error.jsp</param> </result> </action> SendmailResult.java package com.opensymphony.webwork.example; import com.opensymphony.xwork.ActionInvocation; import com.opensymphony.xwork.Result; public class SendmailResult implements Result { private String to; private String from; private String subject; private String body; public void execute(ActionInvocation invocation) throws Exception { //TODO 实现Email发送部分 System.out.println("sending mail...."); System.out.println(" To:" + to); System.out.println(" From:" + from); System.out.println("Subject:" + subject); } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } } 写个简单的测试Action: package com.opensymphony.webwork.example; import com.opensymphony.xwork.ActionSupport; public class TestSendMailAction extends ActionSupport { public String execute() throws Exception { return SUCCESS; } } 测试jsp,把它放在webwork-example/下: testsendmail.jsp <%@ taglib prefix="ww" uri="webwork" %> <html> <head><title>Test sendmail restul type</title></head> <body> <form action="testSendmail.action"> <input type="Submit" value="Submit"/> </form> </body> </html> 打开http://localhost:8080/webwork-example/testsendmail.jsp,提交页面,控制台输出: sending mail.... To:user@sina.com From:root@sina.com Subject:hello,webwork
webwork中action result type
最新推荐文章于 2018-08-06 00:54:07 发布