How to quickly build an MCV program?
Refer to the official spring example: https://spring.io/guides/gs/serving-web-content/
1. Spring MVC combined with thymeleaf template
After creating the maven project, modify the pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.carter659</groupId> <artifactId>spring02</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <name>spring02</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
Add the class file of the controller "MainController.java":
package com.github.carter659.spring02;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;@Controllerpublic class MainController { @GetMapping("/") public String index(Model model) { model.addAttribute("name", "Liu Dong"); return "index"; }}Modify App.java file
package com.github.carter659.spring02;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}Then right-click in the project to enter java build path
Add folder "And Folder"
Add the "resources" folder in the main directory
Modify "Excluded" of "resources":
enter"**"
Create the "templates" folder under src/main/resources and create a new html file "index.html"
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>MVC</title></head><body> <p th:text="'Hello, ' + ${name} + '!'" /></body></html> Enter http://localhost:8080 to check whether it runs successfully:
The above is a dynamic page made using the thymeleaf template. So, how to use static resources in MVC applications?
2. Static resources
Create a new "static" folder under src/main/resources
And copy a picture file in its folder
Modify the previous "index.html" file and add the img tag
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>MVC</title></head><body> <img src="img.png" /> <p th:text="'Hello, ' + ${name} + '!'" /></body></html>At this time, a phenomenon immediately appears:
We found that the program will automatically load, because it depends on "devtools" in maven
Finally, refresh the web page and test whether the static resource is loaded
PS: spring boot mainly promotes the thymeleaf template, while its language uses xml, which I personally think is not very convenient.
Code download: https://github.com/carter659/spring-boot-02.git
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.