springboot 跳转html
程序员文章站
2022-03-10 17:07:55
项目目录结构pom文件 org.springframework.boot spring-boot-starter-thymeleaf
项目目录结构
pom文件
<dependencies><!--pom坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!--jdk-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
application.properties 配置文件
spring.thymeleaf.prefix=classpath:/templates/
index.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"/><title>主页</title></head>
<body><h1>主页</h1></body>
</html>
rule.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"/><title>规则</title></head>
<body><h1>规则</h1></body>
</html>
DemoApplication
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);//初始化服务
}
}
MyController
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("/index")
public String test(){//localhost:8080/index 访问templates下的页面 index.html
return "index";
}
@RequestMapping("/rule")
public String test1(){//localhost:8080/rule 访问templates下的页面 rule/rule.html
return "rule/rule";
}
@ResponseBody
@RequestMapping("/test")
public String test2(){//localhost:8080/test ResponseBody返回字符串
return "这是一个字符串";
}
}
访问页面效果
本文地址:https://blog.csdn.net/weixin_42660202/article/details/112025827