最近在通过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.youkuaiyun.com/YiQieFuCong/article/details/85009401
可能是因为thymeleaf引入的包和书中不同,导致static中的文件没被带上一起跑。在配置项中改动,让他带上了一起跑,问题就解决了。同时路径也改了 th:href="@{/static/style.css}" ,原先的路径识别不了。
万分感谢各路大神的帮助。