webwork中action result type

来源:百度文库 编辑:神马文学网 时间:2024/04/29 06:43:09
和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中:









其大多都有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对象。如:

bar
/foo

执行后接着调用下面的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中定义如下:



action定义:


root@sina.com
user@sina.com
hello,webwork!


error.jsp


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" %>

Test sendmail restul type






打开http://localhost:8080/webwork-example/testsendmail.jsp,提交页面,控制台输出:
sending mail....
To:user@sina.com
From:root@sina.com
Subject:hello,webwork