Java Magazine, March/April 2017
ORACLE COM JAVAMAGAZINE MARCH APRIL 2017 33 ui tools accessor methods that make the JAX RS client available to the rest of the application Controllers Controllers are responsible for handling web requests and responses for an MVC application In other words they bind the view to the data model and provide the necessary business logic for the application A controller class is a CDI managed plain old Java object POJO that contains both a @ Path and a @ Controller javax mvc annotation Controller annotation at the class level or has at least one method annotated with @ Path and @ Controller It should also have at least one method annotated as a resource method using @ GET @ PUT or @ POST A controller is very much the same as a JAX RS class except that it uses diferent annotations to signify that the controller class or method is used for MVC rather than for JAX RS If @ Controller is placed at the class level all methods of the class must be used as MVC controllers However a controller class can be made hybrid by placing the annotation at the method level Hybrid classes can contain both MVC and JAX RS resource methods A controller class is instantiated and initialized for each request so there is no state saved in between requests To save state or data to display in views you must use models which I cover in the next section The @ Path annotation is used to determine which controller class and method is invoked when the URL including the indicated pattern is navigated to from within a browser The return type of a controller method returns the String name of the view to which the browser will navigate upon return The default return type of a controller is text html but it is also possible for the return type to be declared via the @ Produces annotation The code in Listing 4 demonstrates a simple controller class which navigates to the issues xhtml view when the URL http localhost 8080 DukeIssueTracker tracker issues is used Listing 4 @ Path issues @ Controller public class IssuesController @ GET public String displayIssues Perform processing and set Model return issues xhtml The return type of a controller method determines how the request is processed Various return types are possible including Viewable JAX RS Response Redirect and String As mentioned before the default return type is String but the @ View annotation can also be used to indicate the view to which the browser should be directed after processing If it is placed on a method the return type should be void Redirects are possible via the use of the JAX RS Response objects seeOther method or the client redirect prefix return redirect see here Building the controller I now create a class in a new org dukeissuetrackermvc web package named IssuesController This class is annotated with @ Controller and it contains each of the issue tracker resource methods Next I create a method annotated with @ GET from javax ws rs GET that will be used for retrieving all active issues in the tracker I name the method displayIssues and provide a return type of String The @ GET annotation is A controller class is instantiated and initialized for each request so there is no state saved in between requests
You must have JavaScript enabled to view digital editions.