pom文件导入thymeleaf模块
<!--thymeleaf模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
创建controller
package com.buba.controller;
import com.buba.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/toIndex")
public String toIndex(Model model, HttpSession session, HttpServletRequest request){
request.getServletContext().setAttribute("age",21);
model.addAttribute("key","kxj");
session.setAttribute("name","康骁将");
User user = new User();
user.setUsername("陈红");
user.setPassword("123");
model.addAttribute("user",user);
return "index";
}
}
在thymeleaf文件夹下创建index.html,引入thymeleaf的命名空间,然后就可以从后台传值了,我下面列了几个简单的单个传值,对象传值,和session传值,application报红不用管,可以使用.
这些是thymeleaf的默认配置,已经配置好的东西,比如访问路径什么的,所以在访问thymeleaf下面的html的时候不需要加文件夹名和后缀.想改的话自己加配置就可以了.