spring-boot+thymeleaf页面实现

最近在通过spring boot实战一书学习spring boot,遇到了如下问题
xmlns:th="http://www.thymeleaf.org"在程序运行时,打开网页控制器却发现该行代码并没有执行,也没有调用.css文件。
下面附html源代码,build.gradle配置和前端控制器截图

(注:如果我解决了该问题,我会在文后放出原因和解决方案。如果有大神协助,感激不尽。
目前我已经解决了该问题!解决方式放在文末。

<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org">

<head>
     <title>Reading List</title>
    <link rel="stylesheet"  th:href="@{/style.css}" >
</head>

<body>
<h2>Your Reading List</h2>
<div th:unless="${#lists.isEmpty(books)}">
    <dl th:each="book : ${books}">
        <dt class="bookHeadline">
            <span th:text="${book.title}">Title</span> by
            <span th:text="${book.author}">Author</span>
            (ISBN: <span th:text="${book.isbn}">ISBN</span>)

        </dt>
        <dd class="bookDescription">
          <span th:if="${book.description}"
                th:text="${book.description}">Description</span>
            <span th:if="${book.description eq null}">
                No description available</span>
        </dd>
    </dl>
</div>
<div th:if="${#lists.isEmpty(books)}">
    <p>You have no books in your book list</p>
</div>

<hr/>

<h3>Add a book</h3>
<form method="POST">
    <label for="title">Title:</label>
    <input type="text" name="title" size="50"></input><br/>
    <label for="author">Author:</label>
    <input type="text" name="author" size="50"></input><br/>
    <label for="isbn">ISBN:</label>
    <input type="text" name="isbn" size="15"></input><br/>
    <label for="description">Description:</label><br/>
    <textarea name="description" cols="80" rows="5">
        </textarea><br/>
    <input type="submit"></input>
</form>

</body>
</html>

gradle配置:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

网页控制器截图:

在这里插入图片描述问题解决的博客链接:
https://blog.csdn.net/YiQieFuCong/article/details/85009401
可能是因为thymeleaf引入的包和书中不同,导致static中的文件没被带上一起跑。在配置项中改动,让他带上了一起跑,问题就解决了。同时路径也改了 th:href="@{/static/style.css}" ,原先的路径识别不了。
万分感谢各路大神的帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值