---
title: "《研磨struts2》第五章 Result 之 5.7 自定义Result "
author: "研磨struts2"
date: "2012-05-03T08:25:40Z"
tags: []
categories: ["企业架构"]
slug: "struts2-result-5-7-result"
draft: false
lastmod: "2012-05-03T08:25:40Z"
id: 1508122
url: "https://rubbing-struts2.iteye.com/blog/1508122"
---

## 5.7  自定义Result

### []()5.7.1  什么是自定义Result

所谓自定义Result，就是由我们自行开发的Result，而不是使用由Struts2预先定义好的Result。

       在实际开发中，需要自定义Result的机率并不大，因为常见的各种页面展示技术，Struts2都已经预定义好相应的Result了，无需我们自行开发。如果我们要自定义Result，要么就是包装一种新的页面展示技术，要不干脆就是我们自行开发的页面展示技术，出现这些情况的可能性很低。

       虽然用得不多，但是从知识的完整性上，还是需要了解如何自定义Result的，再说，了解自定义Result，也能加深对Result技术的理解。

### []()5.7.2  开发自定义Result

其实，开发自定义Result是非常简单的，只需要实现com.opensymphony.xwork2.Result接口就可以了，这个接口的定义也非常简单，示例如下：

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4056.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4056.html)[打印](http://sishuok.com/forum/blogPost/list/4056.html)

1. public interface Result extends Serializable {
2. public void execute(ActionInvocation invocation) throws Exception;
3. }

就是一个execute方法，在这个方法里面去写Result的真正处理，就是如何展示视图。当然，所有需要的数据都可以从ActionInvocation里面获取到。

       好了，下面来写个示例示范一下，如何开发自定义的Result。为了简单，就采用字符界面来展示视图，简单点说，就是把信息在后台输出一下。

**1：写MyResult类**

示例如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4056.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4056.html)[打印](http://sishuok.com/forum/blogPost/list/4056.html)

1. package cn.javass.action.action;
2. import com.opensymphony.xwork2.ActionInvocation;
3. import com.opensymphony.xwork2.Result;
4. public class MyResult implements Result{
5. public void execute(ActionInvocation invocation) throws Exception {
6. System.out.println("要处理的Result字符串是="+invocation.getResultCode());
7. }
8. }

只是简单的输出了Action返回的要处理的Result的字符串的值，至于要想获取更多要展示的值，可以通过ActionInvocation去获取ActionContext，在ActionContext里面封装着所有需要的值。由于ActionContext在后面才会详细学到，这里就先不去示例了。

**2：在struts.xml中配置使用MyResult**

示例如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4056.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4056.html)[打印](http://sishuok.com/forum/blogPost/list/4056.html)

1. &lt;package name="helloworld"  extends="struts-default"&gt;
2. &lt;result-types&gt;
3. &lt;result-type name="MyResult" class="cn.javass.action.action.MyResult" default="false"/&gt;
4. &lt;/result-types&gt;
5. &lt;action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction"&gt;
6. &lt;result name="toWelcome" type="MyResult"&gt;/s2impl/welcome.jsp&lt;/result&gt;
7. &lt;/action&gt;
8. &lt;/package&gt;

这里面有两个地方要注意：

- 在&lt;action&gt;当前的&lt;package&gt;里面配置了自定义的&lt;result-type&gt;，当然它的配置也很简单，主要就是name和class属性，还有一个default的属性用来表示这个ResultType是否缺省使用的。
- 另外一个地方，在&lt;action&gt;里面配置&lt;result&gt;的时候，要配置type属性，type属性的值就是我们自定义的&lt;result-type&gt;的name属性的值

**3：测试一下**

其它的没有什么变化，照例从登录页面开始运行，去测试一下，看看效果。后台应该有如下输出信息：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4056.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4056.html)[打印](http://sishuok.com/forum/blogPost/list/4056.html)

1. 用户输入的参数为===account=test,password=test,submitFlag=login
2. 要处理的Result字符串是=toWelcome

其中加粗的这句话就是由我们自定义的Result输出的。

       注意：由于此时我们的Result并没有向页面输出任何东西，因此，此时的页面应该是一片空白。

       通过这个示例可以看到，自定义Result也不是一件很困难的事情，困难的是到底如何展示数据，也就是必须有一个相应的视图展示技术，Result就相当于是包装这个视图展示技术的。

 

私塾在线网站原创《[研磨struts2](http://sishuok.com/forum/blogCategory/showByCategory.html?categories_id=81&user_id=5948)》系列

转自请注明出处：【[http://sishuok.com/forum/blogPost/list/0/4056.html](http://sishuok.com/forum/blogPost/list/0/4056.html)】

欢迎访问[http://sishuok.com](http://sishuok.com/)获取更多内容