SpringBoot+Thymeleaf问题

本文解决SpringBoot项目中,使用Thymeleaf模板引擎返回视图时报404错误的问题,同时探讨了Thymeleaf页面中jQuery失效的原因及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot在controller返回数据到thymeleaf报404

用springboot做一个例子,访问controller可以返回数据,但是到thymeleaf却报404,

检查发现路径等没有问题,查阅资料得知

这是因为maven仓库jar包问题,把maven仓库中的所有jar包都删除了,然后重新下载,再启动项目

或者在pom文件的

properties

标签下加入

1

2

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>

<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>

  

Thymeleaf页面的jquery无效

在<head>标签中写的jquery无效

1

2

3

4

5

6

7

8

9

<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head th:include="fragment::header">

    <meta charset="UTF-8">

    <title></title>

    <script>

        ......   // 此处会被覆盖

    </script>

</head>

  

原因:使用  <head th:include="fragment::header">  集中引入外部资源时,会覆盖原页面中的<head>标签,则<script>标签中的代码在页面源码会不存在

引入后的页面源码<head>:

 

1

2

3

4

5

6

7

8

9

10

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <link href="/css/style.css" rel="stylesheet"/>

    <link href="/bootstrap/css/bootstrap.css" rel="stylesheet"/>

    <script src="/js/jquery-3.0.0.min.js" type="text/javascript"></script>

    <script src="/bootstrap/js/bootstrap.js" type="text/javascript"></script>

    <title>fragment</title>

</head>

from:https://www.cnblogs.com/shiysin/p/10569707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值