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

给出运行结果:

最后给出文件目录

相关推荐
星火开发设计11 分钟前
序列式容器:deque 双端队列的适用场景
java·开发语言·jvm·c++·知识
java1234_小锋25 分钟前
Java高频面试题:为什么Zookeeper集群的数目一般为奇数个?
java·zookeeper·java-zookeeper
草履虫建模34 分钟前
Java 集合框架:接口体系、常用实现、底层结构与选型(含线程安全)
java·数据结构·windows·安全·决策树·kafka·哈希算法
坚持就完事了1 小时前
Java泛型
java·开发语言
cyforkk1 小时前
YAML 基础语法与编写规范详解
java
亓才孓1 小时前
[Spring测试]TestRestTemplate
java·后端·spring
逆光的July1 小时前
扫码登录的设计与实现
java
Miqiuha1 小时前
工作答辩框架
java·开发语言
happymaker06261 小时前
Java学习日记——DAY25(JavaSE完结)
java·开发语言·学习
CHANG_THE_WORLD1 小时前
C++指针与引用:从语法到底层的全面剖析
java·数据结构·c++