..
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.
Each method annotated with @ Controller RequestMapping can have a signature varies widely, we see the main input parameters:
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";
}
}
| |
Linux (Course)
Complete guide to open-source system. From 49 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |