如何使用Struts2 REST插件创建自定义方法 [英] How to create custom methods using Struts2 REST plugin

查看:215
本文介绍了如何使用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 Mapping Logic



这个Restful action mapper强制执行Ruby-On- Rails REST样式的映射。
如果未指定方法(通过'!'或'method:'前缀),
方法是猜测使用REST样式约定检查
URL和HTTP方法。我们特别注意确保这个
映射器能够正确使用代码隐藏插件,这样就不需要XML
配置了。

RESTful URL Mapping Logic

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的Together Configuration



如果你想保留使用一些非RESTful URL和REST
的东西,然后你必须提供一个利用
到映射器的配置。

REST and non-RESTful URL's Together Configuration

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.

插件包含自己的配置。如果你查看Rest
插件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.






最后,您可能无法通过方法指定方法: 前缀,因为它受默认配置的限制。


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

这篇关于如何使用Struts2 REST插件创建自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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