..


Sponsored Links

Web - Controller

As mentioned in the previous paragraph controllers dealing with service requests that are provided by DispatchServlet.
To indicate which requests should be sent to their methods in each controller, there are a number of @ RequestMapping. In addition to defining the path for which a particular method should be invoked, we can define the HTTP method for which to perform the request (POST, GET):






 @ RequestMapping (value = "/" method = RequestMethod.GET)







 public String welcome (Model model) {



  



 model.addAttribute (b);



  



 return "/ book / createBook";







 }



If there is a speficifata @ RequestMapping at the class level all the specified path will be considered absolute, however, if this annotation is specified, all paths are considered relative, for example:





 @ Controller







 {public class ExampleController



	

  



 @ RequestMapping (value = "/")



  



 public String welcome (Model model) {



    



 model.addAttribute (new Book ());



    



 return "/ book / createBook";



  



 }







 }



the path for which the method is invoked http://..../ dispatcherServlet mapping {} /, but for





 @ Controller







 @ RequestMapping (value = "/ book")







 {public class ExampleController



	

  



 @ RequestMapping (value = "/")



  



 public String welcome (Model model) {



    



 model.addAttribute (new Book ());



    



 return "/ book / createBook";



  



 }







 }



the path for which the method is invoked http://..../ dispatcherServlet mapping} {/ book /.

The path is expressed through the @ RequestMapping can also be written in this form

 



 / Path / {param}

 
where param can be a value determined dynamically.
The paths of this type are called URI Templates and are generally used to create services, Rest. It leaves the reader with any depth on the subject.

Each method annotated with @ Controller RequestMapping can have a signature varies widely, we see the main input parameters:

  • Request and Response of the Servlet API as HttpServletRequest.
  • HttpSession to log on.
  • org.springframework.web.context.request.WebRequest, an alternative way to access the request parameters.
  • Parameters annotated with @ @ PathVariable for RequestMapping with paths like / path / {param}
  • Parameters annotated with @ RequestParam, whose value is taken from the request.
  • Parameters which are then exposed to the view: Model, Map, and ModelMap. By setting parameters in these objects are added as attributes to the request, and then displayed, for example, in a jsp.
  • Errors and BindingResult for form validation.

Even the values ​​of return you the methods vary, but for simplicity exhibition, in our examples we will use only the String type.






 @ Controller







 RequestMapping @ ("example")







 {public class FullController



	

  



 RequestMapping @ ("m1")



  



 public String m1 (HttpServletRequest request, HttpServletResponse response) {



    



 / / Business logic



    



 return "/ path";



  



 }



	

  



 RequestMapping @ ("m2")



  



 public String m2 (HttpSession session) {



    



 / / Business logic



    



 return "/ path";



  



 }





  



 RequestMapping @ ("m3")



  



 public String m3 (WebRequest request) {



    



 / / Business logic



    



 return "/ path";



  



 }



	

  



 RequestMapping @ ("m4 / {param}")



  



 public String m4 (@ String PathVariable param) {



    



 / / Business logic



    



 return "/ path";



  



 }





  



 RequestMapping @ ("m5")



  



 public void m5 (Model model, BindingResult result) {



    



 / / Business logic



    



 return "/ path";



  



 }







 }



Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links