Springboot Study of MVC

Source: Internet
Author: User

Spring boot is ideal for Web application development. We can easily create self-contained HTTP servers using embedded Tomcat,jetty or undertow. Most Web applications will start and run quickly using the Spring-boot-starter-web module.

  I. About MVC (Controller) in Springboot

The use of MVC in Springboot is basically consistent with SPRINGMVC, and we can even follow the standards in SPRINGMVC to accomplish the implementation of the Controller.

code example:

 PackageCom.bdqn.lyrk.study.springboot.controller;ImportLombok. Allargsconstructor;ImportLombok. Data;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;/** * @authorChen.nie*/@Controller @requestmapping ("/index") Public classIndexcontroller {@GetMapping ("/index")     PublicString Index () {return"Index"; } @GetMapping ("/number/{number}/desc/{desc}") @ResponseBody PublicBeanentity Bean (@PathVariable ("number")intNumber, @PathVariable ("desc") String desc) {return Newbeanentity (NUMBER,DESC); }} @Data @allargsconstructorclassbeanentity {Private intNumber ; PrivateString desc;}
View Code

When we access the browser address, we get the corresponding result:

We can find here with SPRINGMVC Controller writing no two, the rest of the service layer and DAO layer are also in accordance with the general wording, with @service and @repository marked service and DAO can be

Two. About MVC in Springboot (static resource-view)

  By default, Spring boot provides static content from a directory named/static (or/public or/resources or/meta-inf/resources) in the classpath or ServletContext root directory.

In the static content we can put the Js,css style and other files, in addition to Web services, we can also use Spring MVC to provide dynamic HTML content. Spring MVC supports a variety of template technologies, including Thymeleaf,freemarker and JSPs. Of course Springboot does not recommend using JSP as the view layer, usually we put the template under the Src/main/resources/templates

The following directory is the typical template and static resource directory structure, according to the above rules we put the static resource JS file in the static directory, template file (here is used Freemarker) in the specified directory:

  

 Three about Spring-boot-devtools

When using Springboot, sometimes we want to be able to change the relevant resources in real-time, especially when updating the view resources, particularly prominent, then we can use Springboot to provide us with developer tools to achieve this effect, Automatically restarts when a file on the Classpath changes:

Gradle Code:

Dependencies {    compile ("Org.springframework.boot:spring-boot-devtools")}
View Code

Exclude resources:

If you want to customize these exclusions to automatically restart when a file on the classpath changes, you can use the Spring.devtools.restart.exclude property.

To monitor additional paths:

When you make changes to files that are not in the classpath, you need to restart or reload the application, and you can use the Spring.devtools.restart.additional-paths property to configure additional paths to monitor the changes

Springboot Study of MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.