`
Rainbow702
  • 浏览: 1073126 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类

struts2中,在使用 convention 插件的情况下,如何使用 “chain” 这个result

阅读更多

执行完一个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 

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

 

分享到:
评论

相关推荐

    Struts2插件convention

    在Struts2框架中,"Convention"插件是其核心组件之一,它引入了一种基于约定优于配置(Convention over Configuration)的开发模式,极大地简化了应用的配置过程。这种模式使得开发者无需编写大量的XML配置文件,...

    如何使用struts2的零配置插件convention

    通过在`struts.xml`中配置`<convention-default-result-type>`或`<convention-result-type-mapping>`元素,可以自定义结果类型和结果模板的映射。另外,可以使用`<convention-result/>`元素来为特定的Action或Action...

    struts2-Convention插件使用

    在项目中结合使用Convention插件和REST风格的URL,可在`struts.xml`中配置如下常量: ```xml <constant name="struts.convention.action.suffix" value="Controller"/> <constant name="struts.convention.action....

    struts2的Convention插件说明书(中文版)

    默认情况下,结果页面位于`WEB-INF/content`目录下,但可以通过`struts.convention.result.path`属性自定义。 4. **命名空间约定**: - 包名会被映射为命名空间(namespace)。例如,`com.example.actions`可能...

    struts2的convention配置详解 很全

    在Struts2中,Convention插件就是这一原则的具体实现。它默认为类、方法和Action提供了一套规则,使开发者能够快速创建可运行的应用,而无需手动编写大量的Struts.xml配置文件。 1. **Action的自动映射**:在...

    struts2的插件使用

    这篇博文主要探讨的是Struts2中的"convention-plugin",这是一个自动配置插件,极大地简化了开发过程。 在传统的Struts2应用中,开发者需要手动编写XML配置文件来定义Action类和结果页面的映射。然而,随着...

    struts2-convention-plugin-2.3.32.jar

    在给定的文件"struts2-convention-plugin-2.3.32.jar"中,我们关注的是Struts2的Convention插件,版本号为2.3.32。这个插件是Struts2框架的一个重要组成部分,旨在提供更加灵活和自动化的配置方式。 Convention...

    STRUTS2 Convention零配置

    为了在项目中启用Convention插件,首先需要添加`struts2-convention-plugin`到项目的依赖中。对于Struts2.1.6版本来说,需要将`struts-Convention-plugin-2.1.6.jar`文件放置于项目的`WEB-INF/lib`目录下。 #### 2....

    Struts2-rest插件(有注释)

    从 Struts 2.1 开始,Struts 2 改为使用 Convention 插件来支持零配置。Convention 插件彻底地抛弃了配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是由 Struts 2 根据...

    Struts2_Convention_Plugin中文文档

    这个插件引入了一种约定优于配置(Convention over Configuration)的理念,允许开发者在不编写大量XML配置文件的情况下,快速搭建基于Struts2的应用程序。以下是关于Struts2 Convention Plugin的详细知识点: 1. *...

    struts2的convention插件使用.docx

    Struts2的Convention插件是其框架中的一个重要组成部分,旨在简化配置,提供一种自动映射Action类到URL路径的方法,使得开发更加高效。以下是关于Conventio

    MyFramework - struts2 零配置:convention

    首先,要在项目中使用Struts2的Convention插件,你需要在项目的`pom.xml`或`build.gradle`文件中添加对Struts2-convention相关的依赖。对于Maven项目,这可能如下所示: ```xml <groupId>org.apache.struts ...

    Struts2 Convention Plugin中文文档 Annotion

    Struts2 Convention Plugin 是从 Struts2.1 版本开始引入的一个插件,它的主要目标是实现 Struts2 框架的零配置。通过约定优于配置的原则,开发者可以更加专注于业务逻辑,减少大量的 XML 配置工作。以下是 ...

    struts2零配置convention-plugin

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...

    Struts2+Convention+Plugin中文文档

    在Struts2中,Convention插件是一个自动化配置工具,它极大地简化了应用的配置过程,使得开发更加高效。 **Struts2框架的核心特性:** 1. **灵活的Action映射**:Struts2允许通过URL直接映射到Action,支持通配符和...

    struts2采用convention-plugin实现零配置

    在传统的Struts2配置中,开发者需要为每个Action编写XML配置文件,以指定Action类、结果页面和其他相关设置。然而,随着版本的更新,Struts2引入了一个名为Convention Plugin的新特性,旨在简化配置过程,实现所谓的...

    Struts2 Tiles JSON jQuery Convention

    这个"Struts2 Tiles JSON jQuery Convention"的资料包可能是为了展示如何在Struts2框架下结合Tiles、JSON和jQuery实现高效、灵活的Web应用开发。下面将详细解释这些技术的结合以及它们如何协同工作。 1. **Struts2*...

    简述STRUTS2_Convention零配置

    首先,要启用Convention插件,你需要在项目的WEB-INF/lib目录下添加struts-Convention-plugin-2.1.6.jar文件。然后,框架会自动扫描特定包下的Java类,寻找符合Action条件的类。这些条件包括实现了`...

    struts2-convention-plugin-2.3.15.1.jar

    struts2-convention-plugin-2.3.15.1.jar

Global site tag (gtag.js) - Google Analytics