struts注释@Result应该只是在类级别吗?

Xuz*_*ang 5 java configuration struts2 struts2-convention-plugin struts-action

我正在阅读Struts2文档,发现其文档中存在一些矛盾.在此链接https://struts.apache.org/docs/convention-plugin.html

Convention插件允许操作类为操作定义不同的结果.结果分为两类,全球和地方.全局结果在操作类中定义的所有操作之间共享.这些结果被定义为动作类的注释.本地结果仅适用于定义它们的操作方法

但是,在另一个链接https://struts.apache.org/docs/result-annotation.html中,建议:

@Result注释位于Action类级别而不是方法级别.这与基于XML的Action配置中的内容相匹配.不要试图诠释你的行动方法; 不起作用.

哪一个是正确的?可以@Result在方法级别定义吗?

Rom*_*n C 4

本地结果使用属性配置到操作配置@Action。换句话说,本地结果是在允许的地方配置的。使用@Action注释指定属性results列表。您可以在此处添加@Result注释。

Dave Newton 的书《Apache Struts 2 Web 应用程序开发》中有一段摘录:

我们还可以使用 Convention 的注释来配置结果。我们不必依赖Convention 插件的想法来确定我们的结果JSP 文件应该命名什么。我们可以使用 @Result注释手动定义结果,@Results如果需要多个结果,则可以使用注释。@Results (我们只能在类级别使用注解,而@Action@Actions注释可以在方法级别使用。我们可以通过注解的属性在操作级别定义多个结果。@Actionresults

wiki的定义也是正确的

全局结果在操作类中定义的所有操作之间共享。这些结果被定义为操作类的注释。本地结果仅适用于定义它们的操作方法。以下是不同类型结果注释的示例:

com.example.actions.HelloWorld

package com.example.actions;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

@Results({
  @Result(name="failure", location="fail.jsp")
})
public class HelloWorld extends ActionSupport {
  @Action(value="/different/url",
    results={@Result(name="success", location="http://struts.apache.org", type="redirect")}
  )
  public String execute() {
    return SUCCESS;
  }

  @Action("/another/url")

  public String doSomething() {
    return SUCCESS;
  }
}
Run Code Online (Sandbox Code Playgroud)