---
title: "struts2中，在使用 convention 插件的情况下，如何使用 “chain” 这个result"
author: "Rainbow702"
date: "2015-06-10T19:29:08Z"
tags: []
categories: ["编程语言"]
slug: "struts2-convention-chain-result"
draft: false
lastmod: "2015-06-10T19:29:08Z"
id: 2218653
url: "https://rainbow702.iteye.com/blog/2218653"
---

执行完一个Action之后，一般就是跳转至某个JSP页面之类的，但在某些情况下，也有执行完一个Action之后需要跳转至另一个Action继续执行。

比如，使用 addUser 这个Action 新增一个用户之后，我们可能需要使用  userList  这个Action跳转至用户一览画面。

上面这种需求，在使用xml格式的配置文件时，是很容易配置的。所以，此处就不列出了。

此处想说一下，在使用 convention 插件的情况，如何通过 注解来实现。

基于注解方式，又分为两种情况：

 

第一种：  在 method 级别使用了 @Action 注解来指明该 method 是用来处理哪个  action 的，

这种情况下的写法，请参见：  [http://jis225.blog.163.com/blog/static/57329156201152881839409/](http://jis225.blog.163.com/blog/static/57329156201152881839409/ "http://jis225.blog.163.com/blog/static/57329156201152881839409/")

 

第二种：  不使用@Action注解去标注 method， 而是通过以 “!” 的方式去动态调用一个 method。下面以实例来说明一下：

 

```
@Results({
        @Result(name = "listMajorKind", location = "/jsp/kind/major/list.jsp")
        })
public class KindAction {
    public String queryMajorKind() {
        return "listMajorKind";
    }

    public String addMajorKind() {
        return "queryMajorKind";
    }
}
```

对于上面这段代码 ，在使用 convention 的前提下，我们知道：

 

“kind!queryMajorKind” 这个URL最终会跳转至 “/jsp/kind/major/list.jsp” 页面。

那对于“kind!addMajorKind” 这个URL，如果我也想让其最终跳转至上面这个JSP页面的话，应该怎么Result呢？

我先把配置贴出来吧，然后对着代码解释一下：

 

```
@Results({
        @Result(name = "listMajorKind", location = "/jsp/kind/major/list.jsp")，
        @Result(name = "queryMajorKind", type = "chain", params = { "namespace", "/", "actionName", "kind", "method", "queryMajorKind" })
        })
public class KindAction {
    public String queryMajorKind() {
        return "listMajorKind";
    }

    public String addMajorKind() {
        return "queryMajorKind";
    }
}
```

细心的朋友已经注意到了，我多加了一行：

 

 

```
@Result(name = "queryMajorKind", type = "chain", params = { "namespace", "/", "actionName", "kind", "method", "queryMajorKind" })
```

 这个配置的就跟 XML 格式的配置其实是一样的，其中：

 

- “chain” 指定了这个 result的类型是去调用是另一个  action
- 那么，这个action它的名字是什么，它的命名空间、动态调用的方法又是什么？该怎么设置呢？答案就是通过 params 这个参数来设置。对于params，可以看一下如下的API说明：

 

```
The parameters passed to the result. This is a list of strings that form a name/value pair chain since creating a Map for annotations is not possible. An example would be: {"key", "value", "key2", "value2"}.
```

那么，我们又如何知道应该往params中放入哪些参数呢？这个时候，就要去看代码了。

我们知道“chain”类型的result，它的处理类是“com.opensymphony.xwork2.ActionChainResult”，这个类的API说明里道出它需要哪些参数：

```
This result invokes an entire other action, complete with it's own interceptor stack and result. This result type takes the following parameters: 

・actionName (default) - the name of the action that will be chained to 
・namespace - used to determine which namespace the Action is in that we're chaining. If ・namespace is null, this defaults to the current namespace 
・method - used to specify another method on target action to be invoked. If null, this defaults to execute method 
・skipActions - (optional) the list of comma separated action names for the actions that could be chained to 
```

 看到这，应该明白上面的配置了吧。