是否可以在 Struts2 中使用通配符方法调用与仅使用注释的约定插件? [英] Is it possible to use wildcard method invocation in Struts2 with conventions plugin using only annotations?

查看:20
本文介绍了是否可以在 Struts2 中使用通配符方法调用与仅使用注释的约定插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在 struts.xml 中使用通配符方法调用,但是是否可以使用注释来做到这一点?如果有怎么办?

I know how to use wildcard method invocation within struts.xml, but is it possible to do this with annotations? If so how?

推荐答案

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

You probably have it resolved by now, but for those looking for an answer, yes it is possible.

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

要从 url 读取参数,请使用以下注释您的方法:

To read params from the url, annotate your method with this:

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;
}

  • 您需要一个用于 id 参数的 getter/setter.
  • 您需要指定结果,因为 struts2 不知道使用什么来成功获取 url: ...edit/123,因此需要使用 @Result 指向您的文件.有点不符合约定插件的目的.
  • 如果我想重定向到特定的 url,请使用此注释:

    In the case I want to redirect to a specific url, use this annotation:

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

    您需要一个 getter/setter 用于 entityId(在我的例子中是字符串).

    You would need a getter/setter for the entityId (String in my case).

    您还可以拥有高级通配符映射、命名空间通配符映射...

    You can also have advanced wilcard mapping, namespace wildcard mapping ...

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

    Don't forget to change the struts.xml and add the following constants.

    <!-- 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" />
    

    这篇关于是否可以在 Struts2 中使用通配符方法调用与仅使用注释的约定插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆