Spring Web MVC
Spring MVC is a web framework based on the model–view–controller pattern. It is based on the Spring principles and supports a wide range of server-side rendering technologies as JSP, Thymeleaf, Groovy, among others.
Dispatcher Servlet
It is a front controller that coordinates all the request handling activities. Delegates to the web infrastructure beans and invokes the user’s web components. It is automatically created and configured by Spring Boot. If you are not using Spring Boot, you need to add a ViewResolver bean definition and the @EnableWebMvc annotation.
Controller
Annotate controllers with @Controller and annotate methods in the controller with @RequestMapping or @GetMapping to tell Spring what method to execute when processing a particular HTTP GET request. Controllers typically return a logical view name as String.
In the next example when calling http://localhost:8080/ it would be redirected to index.html and when calling http://localhost:8080/hello it would be redirected to welcome.html.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @Controller public class HelloController { @RequestMapping ( "/" ) public String home() { return "index" ; } @RequestMapping ( "/hello" ) public String hi(Model model) { model.addAttribute( "name" , "Spring Boot" ); return "welcome" ; } @RequestMapping ( "/showAccount" ) public String accountDetails( @RequestParam ( "entityId" ) long id, Model model) { ... } @GetMapping ( "/accounts/{accountId}" ) public String show( @PathVariable ( "accountId" ) long accountId, Model model) { ... } } |
The controller method parameters are provided by Spring. In the previous example, the model parameter is used for sending data to the view. You can use HttpServletRequest for the request, HttpSession for session access, Principal for the authenticated user, etc. See Handler Methods Documentation
Use the @RequestParam annotation to extract parameters from the request. In the above example, you can send the entityId parameter calling to http://localhost:8080/showAccount?entityId=1.
To extract the value from the request URL, you can use the {…} placeholders and the @PathVariable annotation. In the above example, you can send the accountId parameter calling to http://localhost:8080/accounts/1. In this case, the annotation value (“accountId”) after the @PathVariable is unnecessary because it matches the parameter name.
View
A view renders web output. It could be an HTML or JSP file. ViewResolvers select the view based on the view name returned by the controller. In Spring Boot, you just need to add the dependency for the ViewResolver (Mustache, Thymeleaf, Groovy, etc)
For instance, this would be the dependency for mustache:
1 2 3 4 | <dependency> <groupId>org.springframework.boot< /groupId > <artifactId>spring-boot-starter-mustache< /artifactId > < /dependency > |
And this would be the welcome.html file:
1 2 3 4 5 6 7 | < code >< span class = "cp" ><! DOCTYPE html></ span > < span class = "nt" >< html </span> < span class = "na" >lang=</ span >< span class = "s" >"en"</ span >< span class = "nt" >></ span > < span class = "nt" >< body ></ span > < span class = "nt" >< div ></ span >Hello {{name}}< span class = "nt" ></ div ></ span > < span class = "nt" ></ body ></ span > < span class = "nt" ></ html ></ span > </ code > |
According to the controller, this would print “Hello Spring Boot”, taking the name attribute from the model.
Published on Java Code Geeks with permission by Eidher Julian, partner at our JCG program. See the original article here: Spring Web MVC Opinions expressed by Java Code Geeks contributors are their own. |