Show Topics
Hide Topics

Spring Boot MVC

In old Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP requests. Methods in your controller are mapped to HTTP using @RequestMapping annotations.

 

SpringMVC example @RestController to serve JSON data

 

 

 

Spring Boot MVC Example

Extract, import to eclipse as Existing Maven project, & Run asmaven install

 

If we see the Folder Structure

  • all index, welcome files must be placed unter →resources\static\
  • all the result pages must be placed under→resources\templates\

 

1.Choose the SpringBoot Stater Dependencies and place in pom.xml & build the project.

 

 

2.Create index.html to provide user input

 

 

3.Create Controller to handle request given by user (/hello)

Here Model is a interface which contains some useful method to return result data to result page

     

                                                   

4.Create result.html template to display the Results given by Controller

 

 

5.Create Application.java to Start & Run Spring Boot Application

 

 

 

6.Start the Application by Run asJava Application (Application.java)

 

7.Open browser, access localhost:8080 the Output should be as below

 

 

Explanation

1.On Running Applicatio.java, Spring Boot Engine Starts and reads the all files in the projects and autowires the data and auto configures the Controller details

 

2.on submitting the form, Spring Boot Searchers for controller classes which are annotated with @Controller

 

3.compairs (“/hello”) path with controller @RequestMapping("/hello"), if matches execute the business logic method and it returns the resultpage name(“result”)

 

4.SpringBoot Engine Searches the appropriate resultpage template having result” as page name & displays the result.html page to the user

 

 

 

1.Model Interface

Model interface designed for adding attributes to the model. Allows for accessing the overall model as a java.util.Map.

Method Summary
 Model addAllAttributes(Collection<?> attributeValues) 
          Copy all attributes in the supplied Collection into this Map,
 Model addAllAttributes(Map<String,?> attributes) 
          Copy all attributes in the supplied Map into this Map.
 Model addAttribute(Object attributeValue) 
          Add the supplied attribute to this Map using a generated name.
 Model addAttribute(String attributeName, Object attributeValue) 
          Add the supplied attribute under the supplied name.
 Map<String,Object> asMap() –       Return the current set of model attributes as a Map.
 boolean containsAttribute(String attributeName) 
          Does this model contain an attribute of the given name?
Model mergeAttributes(Map<String,?> attributes)

 

 

2.Static Content

By default, Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources)

 

You can also customize the static resource locations using spring.resources.static-locations (replacing the default values with a list of directory locations).

 

If you do this the default welcome page detection will switch to your custom locations, so if there is an index.html in any of your locations on startup, it will be the home page of the application.