如何使用 Struts2 REST 插件创建自定义方法

我的问题涉及在操作中创建自定义方法.我正在使用 Struts2 和 REST 插件来实现 RESTful WebService.我的动作类如下:

My problem concerns the creation of a custom method within an action. I'm using Struts2 and REST Plugin in order to implement a RESTful WebService. My action class is the following:

public class SampleController implements ModelDriven<Object> {

private Sample sample = new Sample();
private Collection<Sample> list;
private int id;

public HttpHeaders create() {
    sdao.save(sample);
    return new DefaultHttpHeaders("create");
}   


public HttpHeaders destroy() {
    return new DefaultHttpHeaders("destroy");
}

public HttpHeaders show() {
    return new DefaultHttpHeaders("show").disableCaching();
}

public HttpHeaders update() {
    sdao.save(sample);
    return new DefaultHttpHeaders("update");
}

public HttpHeaders index() {
    list = sdao.findAll();
    return new DefaultHttpHeaders("index").disableCaching();
}

public Object getModel() {
    return (list != null ? list : sample);
}


public int getId() {
    return id;
}

public void setId(Integer id) {
    if (id != null) {
        this.sample = (Sample) sdao.findById(id);
    }
    this.id = id;
}

}

我可以通过 GET HTTP 方法正确访问资源.为了使用自定义方法,通过将参数传递给搜索资源来调用,即

I can access to a resource via a GET HTTP method correctly. In order to use a custom method, called by passing a parameter to search resources i.e

public searchBySenderName(String senderName) {
    list.addAll(sdao.findBySenderName(senderName))
}    

正确的程序是什么?如何通过 GET 以下 URL 调用它?

What is the correct procedures? How can I call it via GET following URL?

推荐答案

您可以从 GET 的任何预定义方法(index, show) 在您的情况下,请参阅 RESTful URL 映射逻辑 .

You can call custom method from any of the predefined methods for GET (index, show) in your case, see RESTful URL mapping logic .

RESTful URL 映射逻辑

这个 Restful 动作映射器强制执行 Ruby-On-Rails REST 样式的映射.如果未指定方法(通过 '!' 或 'method:' 前缀),则方法是猜测的";在使用 REST 风格的约定来检查URL 和 HTTP 方法.已给予特别注意以确保这一点映射器与代码隐藏插件一起正常工作,以便 XML无需配置.

This Restful action mapper enforces Ruby-On-Rails REST-style mappings. If the method is not specified (via '!' or 'method:' prefix), the method is "guessed" at using REST-style conventions that examine the URL and the HTTP method. Special care has been given to ensure this mapper works correctly with the codebehind plugin so that XML configuration is unnecessary.


当然,您可以更改动作映射器使用的方法名称,但这会影响整个应用程序.如果您已经占用了一个资源 URL,那么您应该使用另一个来执行它的工作.这是在您使用严格的 rest 映射器的情况下.在混合模式你可以将通常的操作映射到某个操作方法.


Of course you can change the method names used by the action mapper, but it will affect a whole application. If you already occupied a resource URL then you should use another to perform its job. This is in case if you are using a strict rest mapper. In the mixed mode you can map an usual action to some action method.

REST 和非 RESTful URL 的共同配置

如果您想在 REST 旁边继续使用一些非 RESTful URL东西,那么你必须提供一个配置,利用给映射器.

If you want to keep using some non-RESTful URL's alongside your REST stuff, then you'll have to provide for a configuration that utilizes to mappers.

插件包含自己的配置.如果你看剩下的插件 jar,您将看到 struts-plugin.xml 并在其中看到插件所做的一些配置设置.通常,插件只是按照它想要的方式设置事物.您可能经常需要覆盖这些设置在您自己的 struts.xml 中.

Plugins contain their own configuration. If you look in the Rest plugin jar, you'll see the struts-plugin.xml and in that you'll see some configuration settings made by the plugin. Often, the plugin just sets things the way it wants them. You may frequently need to override those settings in your own struts.xml.


最后,您可能不会通过 !method: 前缀指定方法,因为它受到默认配置的限制.


And last, you mightn't specify a method via ! or method: prefix because it's restricted by default configuration.

相关文章