【发布时间】:2020-05-30 21:18:00
【问题描述】:
我花了将近一天的时间试图解决这个问题,但没有任何运气
我正在集成spring boot mongodb和thymeleaf
这是我的控制器
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@GetMapping("/items")
public String home(Model model) {
model.addAttribute("items", new ItemEntity());
List<ItemEntity> items = itemService.findAll();
model.addAttribute("items", items);
return "item";
}
}
这是我的 html 模板
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"/>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2>FileUpload</h2>
<form action="" th:action="@{'/fileupload'}" th:object="${item}" method="post"
enctype='multipart/form-data'>
<div class="form-group">
<label for="fileupload">File Upload(excel or csv)</label>
<input type="file" class="form-control" id="file" name="file" th:field="*{file}"/>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<div class="col-md-12">
<h2>All items</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>height</th>
<th>width</th>
<th>length</th>
<th>weight</th>
<th>securityLevel</th>
<th>file type</th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${items}">
<td th:text="${item.height}">John</td>
<td th:text="${item.width}">Doe</td>
<td th:text="${item.length}">john@example.com</td>
<td th:text="${item.weight}">john@example.com</td>
<td th:text="${item.securityLevel}">john@example.com</td>
<td th:text="${item.fileType}">john@example.com</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</body>
</html>
build.gradle
plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.vending'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'com.opencsv', name: 'opencsv', version: '4.4'
compile group: 'org.apache.poi', name: 'poi', version: '3.9'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.thymeleaf', name: 'thymeleaf', version: '2.0.5'
compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '1.2'
}
test {
useJUnitPlatform()
}
我在 application.properties 中也有这些用于 mongod
spring.data.mongodb.database=vending
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
当我运行 VendingApplication 并转到 http://localhost:8080/items 时,我得到了这个
白标错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。
2020 年 2 月 15 日星期六 03:26:41 EST 出现意外错误(类型=未找到,状态=404)。 没有可用的消息
看起来 spring 由于某种原因无法解析模板视图
更新:
问题来自没有映射我的数据库名称
application.properties
spring.data.mongodb.database=your_db_name
【问题讨论】:
标签: mongodb spring-boot gradle intellij-idea thymeleaf