idea利用SpringMVC框架整合ThymeLeaf

简洁一些:两个重要文件

1.controller指定html文件:我们访问http://localhost:8080/test

复制代码
package com.example.appledemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {

    @RequestMapping("/test")
    public ModelAndView test(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("hello");
        List<String> list=new ArrayList<String>();
        list.add("yyy");
        list.add("aaa");
        list.add("bbb");
        modelAndView.addObject("list",list);
        return modelAndView;
    }
}

2.templates下创建html文件

复制代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">   <!--需要添加此行标注为thymeleaf模板 -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <th>name</th>
    </tr>
    <tr th:each="name:${list}">
        <td th:text="${name}"></td>
    </tr>

</table>
</body>
</html>

注意:如果创建文件的时候引入了数据库依赖就必须在配置文件中设置url,否则不能成功开启服务器

复制代码
#路径
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html
#编码
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/apple?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#server.servlet.context-path=/demo1
mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis.mapper-locations=classpath:com/demo/cha6_ch7_refactor/dao/*.xml

给出运行结果:

最后给出文件目录

相关推荐
期待のcode15 小时前
Java虚拟机的运行模式
java·开发语言·jvm
程序员老徐15 小时前
Tomcat源码分析三(Tomcat请求源码分析)
java·tomcat
a程序小傲15 小时前
京东Java面试被问:动态规划的状态压缩和优化技巧
java·开发语言·mysql·算法·adb·postgresql·深度优先
仙俊红15 小时前
spring的IoC(控制反转)面试题
java·后端·spring
阿湯哥15 小时前
AgentScope Java 集成 Spring AI Alibaba Workflow 完整指南
java·人工智能·spring
小楼v15 小时前
说说常见的限流算法及如何使用Redisson实现多机限流
java·后端·redisson·限流算法
与遨游于天地15 小时前
NIO的三个组件解决三个问题
java·后端·nio
czlczl2002092516 小时前
Guava Cache 原理与实战
java·后端·spring
yangminlei16 小时前
Spring 事务探秘:核心机制与应用场景解析
java·spring boot
记得开心一点嘛17 小时前
Redis封装类
java·redis