1

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176

1 Answers1

0

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Wrong again. *HttpHeaders is a interface that doesn't extend Result, so it shouldn't be used as a result type.* – Aleksandr M Mar 08 '16 at 14:06
  • 2
    It depends on implementation of `ActionInvocation` - REST plugin provides its own that's why it can handle `HttpHeaders` – Lukasz Lenart Mar 11 '16 at 12:28