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

给出运行结果:

最后给出文件目录

相关推荐
tianyagukechat26 分钟前
rockylinux9.5 配置IP
java·网络·tcp/ip
程序员Sonder34 分钟前
黑马java----正则表达式(一文弄懂)
java·正则表达式·新人首发
doris820435 分钟前
Python 正则表达式 re.findall()
java·python·正则表达式
Anastasiozzzz1 小时前
阿亮随手记:动态条件生成Bean
java·前端·数据库
丹牛Daniel2 小时前
Java解决HV000183: Unable to initialize ‘javax.el.ExpressionFactory‘
java·开发语言·spring boot·tomcat·intellij-idea·个人开发
消失的旧时光-19432 小时前
智能指针(三):实现篇 —— shared_ptr 的内部设计与引用计数机制
java·c++·c·shared_ptr
芒克芒克2 小时前
深入浅出CopyOnWriteArrayList
java
wuqingshun3141592 小时前
说一下java的反射机制
java·开发语言·jvm
A懿轩A2 小时前
【Java 基础编程】Java 异常处理保姆级教程:try-catch-finally、throw/throws、自定义异常
java·开发语言·python
极客先躯2 小时前
高级java每日一道面试题-2025年7月14日-基础篇[LangChain4j]-如何集成开源模型(如 Llama、Mistral)?需要什么基础设施?
java·langchain·存储·计算资源·模型服务框架·网络 / 协议·java 依赖