SpringBoot accesses HTML pages through the background

The content of this article mainly talks about how to enter HTML pages through SpringBoot

1. First add dependencies on HTML in pom.xml

/**
 * pom.xml文件
 */
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>**

2. Add SpringBoot related configuration in the application.yml file

spring:
  thymeleaf:
    prefix: classpath:/templates/   server: port:8080                      context-path:"/lmycc"

3. Create HTML file

/**
 * 路径:resources/templates
 */
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title>第一个HTML页面</title>
</head>
<body>
<h1>Hello Spring Boot!!!</h1>
<p th:text="${hello}"></p>
</body>
</html>

4. Write the method of jumping to the HTML page in the Controller

@Controller  //注意这里必须为Controller
public class HelloController {

    /**
     * 本地访问内容地址 :http://localhost:8080/lmycc/hello
     * @param map
     * @return
     */
    @RequestMapping("/hello")
    public String helloHtml(HashMap<String, Object> map) {
        map.put("hello", "欢迎进入HTML页面");
        return "/index";
    }
}

5. Visit the page 
write picture description here

Well, so you can access HTML pages normally through SpringBoot, let's try it! 
Source code download address: http://download.csdn.net/download/sinat_33889619/10038775

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735085&siteId=291194637