|
Webwork2 之 Action Result Type 和Webwork1.x不同,Webwork2的Action執(zhí)行完后,其Result對(duì)應(yīng)一個(gè) Result Type,而這個(gè)Result Type完全可以根據(jù)具體應(yīng)用或環(huán)境自己進(jìn)行 定義,只需實(shí)現(xiàn)com.opensymphony.xwork.Result接口。Result Type使得Action的執(zhí)行結(jié)果表現(xiàn)形式可以靈活多變!下面這會(huì)舉例說(shuō)明,這里先看看Webwork2提供的幾種Result Type的定義,該定義在webwork-default.xml中,xwork.xml文件包含了該文件,自定義的Result Type可以直接寫(xiě)在 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> 其大多都有l(wèi)ocation和parse兩個(gè)參數(shù),location指明action執(zhí)行后接著去哪里,parse指明是否對(duì)location進(jìn)行OGNL表達(dá)式解析。 1) dispatcher action執(zhí)行完后,請(qǐng)求會(huì)導(dǎo)向?qū)?yīng)的View,Webwork2幕后其實(shí)是用RequestDispatcher來(lái)處理的,所以原Request/Response對(duì)象會(huì)接著傳遞,原Request中的Atrributes不會(huì)丟失,這點(diǎn)與下面的redirect是不同的。 2) redirect 對(duì)上次的響應(yīng)將重定向到指定的位置,redirect是重新產(chǎn)生一個(gè)新的Request,原來(lái)Request保存的東西將不再有效,比如不能通過(guò)requet.getAtrribute 取得原來(lái)set的對(duì)象,也不能取得action的實(shí)例,errors,field errors等,因?yàn)锳ction是建立在Single-thread model上的。 3) chain action鏈,特殊的View調(diào)用方式,一個(gè)action執(zhí)行完接著調(diào)用另一個(gè)action。有個(gè)必須的參數(shù)actionName,指明緊接著調(diào)用的另一action對(duì)象。如: <result name="success" type="chain"> <param name="actionName">bar</param> <param name="namespace">/foo</param> </result> 執(zhí)行后接著調(diào)用下面的action: <action name="bar" class="myPackage.barAction"> ... </action> 4) velocity 5) freemarker 6) jasperreports 7) xslt 以上都是用不同技術(shù)的產(chǎn)生不同的View。 下面我舉個(gè)自定義Result Type的示例,假如我有個(gè)Action testSendmail,根據(jù)處理結(jié)果將給指用戶發(fā)送一份email。自定義一個(gè)Result Type,實(shí)現(xiàn)Result接口。 com.mycompany.webwork.example.SendmailResult 有三個(gè)必須參數(shù):from ,to, subject,一個(gè)可選參數(shù) 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 實(shí)現(xiàn)Email發(fā)送部分 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; } } 寫(xiě)個(gè)簡(jiǎn)單的測(cè)試Action: package com.opensymphony.webwork.example; import com.opensymphony.xwork.ActionSupport; public class TestSendMailAction extends ActionSupport { public String execute() throws Exception { return SUCCESS; } } 測(cè)試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> 打開(kāi)http://localhost:8080/webwork-example/testsendmail.jsp,提交頁(yè)面,控制臺(tái)輸出: sending mail.... To:user@sina.com From:root@sina.com Subject:hello,webwork |
|
|