是否可以在Struts2中使用通配符方法调用,仅使用注释的约定插件?

Qua*_*ion 6 annotations struts2

我知道如何在struts.xml中使用通配符方法调用,但是可以使用注释来执行此操作吗?如果是这样的话?

ont*_*ntk 8

你可能现在已经解决了,但是对于那些寻找答案的人来说,是的,这是可能的.

有关通配符映射,请参阅:http://struts.apache.org/2.3.1.2/docs/wildcard-mappings.html

要从URL中读取params,请使用以下方法注释您的方法:

private String id;

@Action(value = "edit/{id}",
        results={@Result(name = "success", location = "/WEB-INF/views/devices/edit.ftl")}
)        
public String edit() {  
    // do something
    return SUCCESS;
}

public void setId(String id) {
    this.id = id;
}

public String getId() {
    return id;
}
Run Code Online (Sandbox Code Playgroud)
  • 您将需要id参数的getter/setter.
  • 你需要指定结果,因为struts2不知道url:... edit/123成功使用什么,因此需要使用@Result指向你的文件.Kinda在那里摒弃了常规插件的目的.

在我想重定向到特定网址的情况下,请使用此注释:

@Action(value = "index",
        results={@Result(name = "success", location = "/devices/edit/${entityId}", type = "redirect")}
    )
Run Code Online (Sandbox Code Playgroud)

你需要一个针对entityId的getter/setter(在我的例子中是String).

您还可以使用高级wilcard映射,命名空间通配符映射...

不要忘记更改struts.xml并添加以下常量.

<!-- Used for advanced wilcard mapping -->
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />
Run Code Online (Sandbox Code Playgroud)