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

给出运行结果:

最后给出文件目录

相关推荐
大大水瓶6 分钟前
Tomcat
java·tomcat
dustcell.7 分钟前
haproxy七层代理
java·开发语言·前端
游离态指针10 分钟前
以为发消息=下单成功?RabbitMQ从0到秒杀实战的完整踩坑笔记
java
BD_Marathon31 分钟前
工厂方法模式
android·java·工厂方法模式
玹外之音1 小时前
Spring AI MCP 无状态服务器实战:构建AI智能用户管理系统
java·后端
java干货1 小时前
Redis 的 ZipList 是什么?它是怎么解决内存碎片问题的?
java
失重外太空啦1 小时前
Tomcat
java·服务器·tomcat
屎到临头想搅便1 小时前
TOMCAT
java·tomcat
Sylvia33.1 小时前
火星数据:解构斯诺克每一杆进攻背后的数字语言
java·前端·python·数据挖掘·数据分析