`

struts2中Action跳转的convention 配置

 
阅读更多
本action演示了一个action执行完成后,转到另外一个action的情况。
(1)如果需要内部转发:则在要转到的方法前面定义一个新的action名称,名称为:前一方法名!前一方法返回值.
(2)如果需要外部重定向:则定义一个Result,类型必须为"redirect"。
(3)若即定义了外部重定向Result,也定义了内部转发,显调用时的名称相同,则执行外部重定向Result
package com.abc.domain.access.action.common;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope("prototype")
//声明此类为控制层的类,且为prototype模式调用
@ParentPackage(value = "abc-default")
@Results(
{
@Result(name="toShow", location="show.action", type="redirect"),
@Result(name="add", location="add.action", type="redirect")
})


@Namespace("/common/test")
public class TestAction
{

@Actions({
   @Action("show"),
   @Action("one!show")
   }) 
public String show() throws Exception
{  
   return "success";
}

@Actions({
   @Action("add"),
   @Action("two!add")
   }) 
public String add() throws Exception
{  
   return "success";
}

@Action("one")
public String one() throws Exception
{  
  //return "show";   //内部转发到show(),找到名为"one!show"的Action执行
   return "toShow"; //外部重定向到show(),找到名为"toShow"的Result,然后再执行show()
}

@Action("two")
public String two() throws Exception
{  
return "add"; //外部重定向到add(),因为找到了名称为add的Result配置,所以不再去找two!add
}
}
1
0
分享到:
评论

相关推荐

    struts2 中路径无法跳转的问题

    当遇到“Struts2中路径无法跳转的问题”时,这通常与配置、Action类、结果类型或JSP页面的设置有关。以下是一些可能导致问题的关键知识点: 1. **Struts2配置文件**: - 配置文件通常为`struts.xml`,这是定义...

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

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

    Struts2 in action(struts2实战)

    - **Action**:在Struts2中,Action是业务逻辑的载体,负责处理用户请求并返回结果。每个Action对应一个特定的用户操作。 - **Result**:Action执行完毕后,会返回一个Result,定义了页面跳转或数据呈现的方式。 ...

    struts2 使用注解现在零配置不需要在使用struts.xml配置文件,可以直接跑

    在Struts2中,注解的引入使得开发者可以摆脱繁琐的`struts.xml`配置文件,实现“零配置”运行。 首先,让我们了解什么是注解(Annotation)。注解是Java提供的一种元数据机制,允许在源代码中嵌入信息,这些信息...

    struts2 in action 中文版 英文版 源代码 合集

    4. **插件**: Struts2支持众多插件,如Struts2-dojo-plugin用于AJAX功能,struts2-convention-plugin简化配置,struts2-spring-plugin整合Spring框架等。 5. **OGNL表达式**: OGNL(Object-Graph Navigation ...

    struts2 中action的使用

    通过在`struts.xml`中配置拦截器栈,你可以决定Action执行前后需要进行哪些预处理和后处理操作。 7. **结果类型(Result Types)**:Struts2支持多种结果类型,如`dispatcher`(转发到JSP页面)、`redirect`...

    struts2+myeclipse 配置入门

    3. **创建Action类**: Struts2中的业务逻辑由Action类执行。创建一个Action类,实现`com.opensymphony.xwork2.ActionSupport`接口,并重写`execute()`方法。例如: ```java public class HelloWorldAction extends...

    struts2jar.zip

    2. **插件包**:根据项目需求,可能需要其他的插件,如struts2-convention-plugin.jar(用于自动配置)、struts2-json-plugin.jar(支持JSON响应)等。 3. **依赖的第三方库**:Struts2依赖于其他的一些库,如ognl...

    struts2 convention plugin(插件) 测试项目

    Struts2 Convention Plugin是Apache Struts框架的一个重要组成部分,它为开发者提供了一种更为便捷的配置方式,使得在Struts2应用中实现MVC模式变得更加简单。这个测试项目旨在帮助我们理解和掌握如何在实际开发中...

    Struts2需要的jar包和配置文件

    在Struts2中,我们需要在此文件中配置过滤器(`<filter>`和`<filter-mapping>`),以便拦截所有请求并引导它们通过Struts2的生命周期。 2. **struts.xml**:这是Struts2的核心配置文件,定义了动作、结果类型、拦截...

    Struts2主要Lib

    在Struts2中,Action类代表控制器,负责处理用户的请求;模型由业务逻辑组件组成,处理数据操作;视图则负责渲染并展示结果给用户。 2. **Struts2 Action**:Action是Struts2的核心组件,它处理用户请求,并通过...

    struts2框架的简单配置和使用

    Struts2支持动态结果页面,可以根据Action返回的结果值来决定跳转到哪个页面。例如: ```java public String execute() { if (someCondition) { return "success"; } else { return "error"; } } ``` 在struts....

    struts2.x所有包及配置文件

    Struts2是一个基于MVC(Model... - 将这些jar包添加到项目的类路径中,并在web.xml中配置Struts2的过滤器`org.apache.struts2.dispatcher.FilterDispatcher`(在较新版本中是`org.apache.struts2.dispatcher.ng.filter....

    struts2注解必须包

    在Struts2中,注解(Annotation)是开发者常用的一种元数据表示方式,它允许我们在代码中直接标注信息,简化配置文件。"struts2注解必须包"指的是Struts2提供的一系列注解,它们对于简化Struts2应用的配置和增强其...

    struts2入门和配置

    通过在struts.xml中配置Interceptor栈,可以控制拦截器的执行顺序。 6. 结果视图:Struts2支持多种结果类型,如dispatcher(转发)、stream(流式输出)、freemarker(FreeMarker模板引擎)等。你可以根据需求选择...

    struts2注解详解

    可以通过配置项 `struts.convention.package.locators` 来指定搜索的位置,例如:`actions`, `action`, `struts`, `struts2` 等。这表示框架会在这些命名空间下查找 Action 类。此外,还可以通过 `struts.convention...

    struts2核心文件

    例如,`struts2-convention-plugin`提供了自动Action映射的便利。 6. **OGNL(Object-Graph Navigation Language)**: OGNL是Struts2的默认表达式语言,用于在Action和视图之间传递数据。例如,Action中的属性可以...

    struts2-jar包

    在压缩包"struts2"中,可能包含了多个版本的Struts2 jar文件,例如`struts2-core.jar`是核心库,`struts2-convention-plugin.jar`是约定优于配置的插件,`struts2-dojo-plugin.jar`用于Ajax支持等。根据项目的具体...

    struts2-convention-plugin-2.3.4-sources

    5. **依赖注入**:Struts2 Convention插件支持依赖注入,可以自动将服务或DAO注入到Action类中,无需XML配置。它依赖于Spring或Struts2自身的OGNL表达式语言来实现。 6. **插件集成**:Struts2允许添加多个插件,...

    struts2-2.3.16.1安装包及其配置jar

    2. **配置web.xml**:在Web应用的`web.xml`配置文件中,配置Struts2的前端控制器`StrutsPrepareAndExecuteFilter`,使其拦截所有请求。 3. **创建struts.xml**:这是Struts2的核心配置文件,定义了Action、结果类型...

Global site tag (gtag.js) - Google Analytics