执行完一个Action之后,一般就是跳转至某个JSP页面之类的,但在某些情况下,也有执行完一个Action之后需要跳转至另一个Action继续执行。
比如,使用 addUser 这个Action 新增一个用户之后,我们可能需要使用 userList 这个Action跳转至用户一览画面。
上面这种需求,在使用xml格式的配置文件时,是很容易配置的。所以,此处就不列出了。
此处想说一下,在使用 convention 插件的情况,如何通过 注解来实现。
基于注解方式,又分为两种情况:
第一种: 在 method 级别使用了 @Action 注解来指明该 method 是用来处理哪个 action 的,
这种情况下的写法,请参见: 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
看到这,应该明白上面的配置了吧。
相关推荐
- **结果类型的配置**:对于特殊的结果类型(如redirect等),可以在Action类中使用@Result注解进行配置。 - **包级别的配置**:如果需要对整个包内的Action进行统一配置(如使用不同的Interceptor栈),可以在`...
总的来说,`struts2乱码与json插件(1)` 这个主题涵盖了如何在Struts2中处理乱码问题以及如何启用和配置JSON插件以支持JSON数据交换。理解和掌握这些知识点对于开发高效、无乱码的Struts2应用至关重要。通过正确配置...
- **插件依赖**:要启用Struts2的注解支持,首先需要在项目中添加`struts2-convention-plugin`插件。例如,对于Struts2.1.8.1版本,你需要包含`struts2-convention-plugin-2.1.8.1.jar`。 - **配置项**:在项目的`...
Convention插件使得Struts2可以实现“零配置”开发,即通过类名和方法名自动映射Action和结果。例如,类名为`UserAction`的方法`save()`可以直接映射到URL `/user/save.action`。 5. **Struts2注解**: - **...
- **选项D**:如果采用注解方式开发,并且使用了Struts2的Convention插件,那么确实需要额外导入该插件的JAR包。 ### Struts2 MVC模式解析 - **选项A**:正确。Struts2确实是一个基于MVC模式的Web层框架。 - **选项...
2. `struts2-convention-plugin.jar`:提供自动映射和插件配置的便利。 3. `struts2-json-plugin.jar`:支持JSON格式的数据交互,方便AJAX操作。 Hibernate的依赖包括: 1. `hibernate-core.jar`:Hibernate的主要...