Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

java - How to create custom methods using Struts2 REST plugin

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

}

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


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 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...