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

给出运行结果:

最后给出文件目录

相关推荐
汤姆yu1 小时前
基于springboot的在线答题练习系统
java·spring boot·后端·答题练习
我认不到你1 小时前
JVM分析(OOM、死锁、死循环)(JProfiler、arthas、jdk调优工具(命令行))
java·linux·开发语言·jvm·spring boot
zhong liu bin1 小时前
maven【maven】技术详解
java·ide·python·spring·maven·intellij-idea
扯淡的闲人2 小时前
Visual Studio Code的使用简介
ide·vscode·编辑器
七夜zippoe2 小时前
Java 技术支撑 AI 系统落地:从模型部署到安全合规的企业级解决方案(二)
java·人工智能·安全
孤狼程序员2 小时前
异常处理小妙招——1.别把“数据库黑话”抛给用户:论异常封装的重要性
java·数据库·mysql
java干货2 小时前
还在 @AfterEach 里手动 deleteAll()?你早就该试试这个测试数据清理 Starter 了
java·数据库·oracle
弗锐土豆3 小时前
编程基础-eclipse创建第一个程序
java·eclipse·helloworld·创建工程
Akshsjsjenjd4 小时前
Ansible 核心功能:循环、过滤器、判断与错误处理全解析
java·数据库·ansible
桦说编程4 小时前
使用注解写出更优雅的代码,以CFFU为例
java·后端·函数式编程