The emergence of Spring Boot has greatly simplified the initial construction and development process of Spring projects. Today we quickly build a Spring Boot environment with page rendering (themeleaf template engine).
1. First, we create a Maven project in IDEA
Check create from archetype and select webapp
2. Add Spring Boot dependencies and themeleaf dependencies in the pom file
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>1.5.3.RELEASE</version> </dependency>
Then right-click in the pom file and select maven in the reimport to download dependencies from the central repository, and wait for the download to complete. Generally, Maven's default central warehouse is slower. It is recommended to use Alibaba Cloud's central warehouse. You can specify the central warehouse by changing Maven's settings file.
3. Create a new java directory in the src/main directory, click Project Structure in the upper right corner to change the java directory to source format (so that java files can be created in the java directory)
4. Create Spring Boot startup class in the java directory just now
@Controller@EnableAutoConfigurationpublic class SampleController { @RequestMapping("/") public String home(){ return "index"; } public static void main(String argv[]){ SpringApplication.run(SampleController.class,argv); }}The well-known spring mvc will automatically locate the jsp page under the webapp based on the return String, but Spring Boot does not integrate this aspect, so we need to introduce the template engine for page rendering.
5. Because themeleaf template engine loads the page under resources/templates/ by default, we need to create such a path ourselves.
Remember: The sentence <html xmlns:th="http://www.thymeleaf.org"> must be added to the html page, otherwise the themeleaf engine will not be recognized.
Finally, right-click run on the Spring Boot startup class to directly start the built-in tomcat in Spring Boot. A Spring Boot+Spring MVC is completed.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.