springboot加载templates下html
---------------------------------------------------------------------------------------------------------------------------------
前端:如果thymeleaf版本过低,这里<meta charset="UTF-8">可能会报错
因为没有结束的斜线自己加上<meta charset="UTF-8"/>即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<input type="button" id="tiaozhuan" value="跳转" onclick="tiaozhuan1();">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
function tiaozhuan1(){
location.href='tiaozhuanlogin';
}
</script>
</body>
</html>
后台:
package com.example.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@RequestMapping("/login")
public String show(){
return "login";
}
@GetMapping("/tiaozhuanlogin")
public String show2(){
//这里login2 是个html
return "login2";
}
}
注意:注解使用的是@Controller
yml:
spring:
datasource:
name: test #数据库名
#url: jdbc:mysql://localhost:3306/test #url
url: jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8
username: root #用户名
password: Aa12345% #密码
driver-class-name: com.mysql.cj.jdbc.Driver #数据库链接驱动
mybatis:
mapper-locations: classpath*:mapper/*Mapper.xml #配置映射文件
type-aliases-package: com.example.test.bean #配置实体类
#主要是下边这个thymeleaf的配置,上边是我数据源的配置,不用管
#主要是下边这个thymeleaf的配置,上边是我数据源的配置,不用管
#主要是下边这个thymeleaf的配置,上边是我数据源的配置,不用管
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
content-type: text/html
mode: HTML5
pom:
<!--thymeleaf模板引擎配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>