Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should struts annotation @Result only be in class level or not?

I am reading Struts2 documentation and found that there was kind of contradiction in its documentation. In this link https://struts.apache.org/docs/convention-plugin.html

The Convention plugin allows action classes to define different results for an action. Results fall into two categories, global and local. Global results are shared across all actions defined within the action class. These results are defined as annotations on the action class. Local results apply only to the action method they are defined on

However, in another link, https://struts.apache.org/docs/result-annotation.html, it is suggested that :

The @Result annotation lives at the Action class level and not the method level. This matches what is found in an XML-based Action configuration. Do not be tempted to annotate your Action's methods; it will not work.

So which one is correct? Can @Result be defined in a method level?

like image 474
Xuzheng Wang Avatar asked Jan 20 '16 02:01

Xuzheng Wang


1 Answers

Local results are configured to the action config using @Action property. In other words local results are configured where they allowed to. Using @Action annotation you specify a property results list. This is where you can add @Result annotations.

There is a excerpt from the Dave Newton's book "Apache Struts 2 Web Application Development":

We can also configure results with Convention's annotations. We don't have to rely on the Convention plug-in's idea of what our result JSP files should be named. We can define results manually using the @Result annotation, and the @Results annotation if we need multiple results. (We can use the @Results annotation only at the class level, while the @Action and @Actions annotations are available at the method level. We can define multiple results at the action level via the @Action annotation's results property.)

The wiki definition also correct

Global results are shared across all actions defined within the action class. These results are defined as annotations on the action class. Local results apply only to the action method they are defined on. Here is an example of the different types of result annotations:

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;
  }
}
like image 185
Roman C Avatar answered Oct 06 '22 00:10

Roman C