Twitter

luni, 8 februarie 2016

Specifying the controller’s method return type (part I)

The controller's method return type can be void, String, Viewable Java type and Response:
·         void - such controller method must be decorated by @View          
·         String - the returned String will be interpreted as a path
·         Viewable - encapsulates a view path as well as additional information related to its processing    
·         Java Type - the method toString() is called on other Java types and the result interpreted as a view path   
·         Response - A JAX-RS Response whose entity’s type is one of the above

Void and String Examples

In the simplest example we return a String (path) from a controller method, so in this section we will focus more on returning void, using @View and exposing the interaction cases between @View and String returning.  Let's suppose that we have a simple controller with a single method that returns void. In such case we need to annotate the method with @View("path"), as below:

// the helloVoidAction() will return the hello.html thanks to @View
@Controller   
@Path("hello")
public class HelloController {
   
 @GET      
 @View("hello.html")
 public void helloVoidAction() {
  // NOPE       
 }
}

In the above example, we can achieve the same behavior by annotating the class with @View. If all the methods of a controller return void then the @View from class level will point out the same returned view for all methods. Further, let's suppose that we have a controller with @View at class level and with different kind of methods, as follows (notice how the returned path is computed based on certain priorities: the String returned by a method has the highest priority, while the @View at class level has the smallest priority):

·         by default, the controller will always return bye.html
@Controller
@Path("bye")
@View("bye.html")
public class ByeController {
 ...
}

·         method that uses @View at class level (returns the bye.html)
@GET
@Path("forever")
public void byeVoidAction() {
 // NOPE       
}

·         method that override the @View from class level with @View at method level (returns the seeyou.html)
@GET
@Path("wasbye")
@View("seeyou.html")
public void wasForeverVoidAction() {
 // NOPE       
}

·         method that override @View from method level by returning a String (path)  (returns the bye.html)
@GET
@Path("stillbye")
@View("seeyou.html")
public String stillByeStringAction() {
 return "bye.html";
}

·         method that returns null will use @View from class level (returns the bye.html)
@GET
@Path("nullbye")
public String nullByeStringAction() {
 return null;       
}

·         method that returns null will use @View from method level (returns the seeyou.html)
@GET
@Path("nullseeyou")
@View("seeyou.html")
public String nullSeeyouStringAction() {
 return null;       
}

·         method that returns String (path) overrides @View from class level (returns the seeyou.html)
@GET
@Path("fornow")
public String seeyouStringAction() {
 return "seeyou.html";
}

Niciun comentariu:

Trimiteți un comentariu