简体   繁体   中英

Can I map an action method that returns Object in Struts 2

I've used Struts2 to map actions to the methods that return String . Can I use other types? What types are possible to use?

I found that code using a REST plugin

// Handles /orders/{id} GET requests
public HttpHeaders show() {
    model = orderManager.findOrder(id);
    return new DefaultHttpHeaders("show")
        .withETag(model.getUniqueStamp())
        .lastModified(model.getLastModified());
}

It shows that it maps to method show that returns HttpHeaders . And it's not a String . How it works?

The framework has features that allows to return not only String . You can return an instance of the Result directly from the action method instead of a String. For example

public Result method() {
  //todo implementation is here  
}

If needed to return multiple types you can set return type as Object .

public Object method() {
    Object resultCode = "success";
    if (something) {
        resultCode = new StrutsResultSupport();
    }
    return resultCode ;
}

About the rest method HttpHeaders is a interface that doesn't extend Result , so it shouldn't be used as a result type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM