java - 在springboot中传递模板时发生错误
问题描述
我是springboot的新手,我在浏览器上遇到了这个错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed May 06 02:05:58 PDT 2020
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/institute/home.html]" - line 29, col 28)
我的 InstituteController 的代码是:
package com.example.demo.controller;
import com.example.demo.dto.InstituteDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
@Controller
@RequestMapping("/institute")
public class InstituteController {
@GetMapping("/home")
private String instituteSave(Model model) {
model.addAttribute("institute", new InstituteDto());
return "institute/home";
}
@PostMapping("/save")
private String saveInstitute(@Valid @ModelAttribute("institute") InstituteDto instituteDto, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
if (!bindingResult.hasErrors()) {
return null;
} else {
String error = bindingResult.getAllErrors().get(0).getDefaultMessage();
redirectAttributes.addFlashAttribute("error",error);
return "redirect:/institute/home";
}
}
}
我的 InstituteDto 的代码是:
package com.example.demo.dto;
import com.example.demo.enums.Status;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class InstituteDto {
private Long id;
@NotNull(message = "Institute name cannot be null")
@NotBlank(message = "Institute name cannot be blank")
private String name;
@NotNull(message = "Institute address cannot be null")
@NotBlank(message = "Institute address cannot be blank")
private String address;
@NotNull(message = "Institute officialWebsite cannot be null")
@NotBlank(message = "Institute officialWebsite cannot be blank")
private String officialWebsite;
private Status status;
}
我 home.html 的代码是:
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Institute Home page</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
<h3>Form</h3>
<!--/*@thymesVar id="institute" type="institute"*/-->
<form th:action="@{~/institute/save}" method="post" th:object="${institute}">
<div class="form-group">
<label for="institute_name">Institute name</label>
<input type="name" class="form-control" id="institute_name" aria-describedby="institute_name"
th:field="*{name}">
</div>
<div class="form-group">
<label for="address">Institute address</label>
<input type="name" class="form-control" id="address" aria-describedby="address" th:field="*{address}>
</div>
<div class=" form-group">
<label for="officialWebsite">Institute officialWebsite</label>
<input type="name" class="form-control" id="officialWebsite" aria-describedby="officialWebsite"
th:field="*{officialWebsite}>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<div class=" col-md-8">
<h3>List</h3>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script>
</body>
</html>
我查找了一些解决方案,有人说将 Controller 注释更改为 RestController 但这也没有解决我的问题。
我要做的是在网页中绑定错误消息,并从网页中获取模型中的输入。是什么导致了这个错误,我该如何纠正它?
解决方案
推荐阅读
- java - 如何使用 If else 语句 java 到 auth 过滤器
- typescript - 为什么不能急切地解决这个带有通用 keyof 的条件?
- google-apps-script - 使用自定义侧边栏,取选定范围并输出线性比例,并在开始值和结束值之间舍入
- visual-studio - 自从我升级到 VS2019 后,我的构建代理没有接收任何 C# 任务
- css - Flex 和布局(React Native)
- node.js - 如何使用nodejs将文件下载到本地计算机?
- javascript - 在页面上获取 Youtube 视频 ID 并设置为播放列表参数
- linux - 如何打开 xterm -e 'command',保留已声明的函数?
- deep-learning - DQN - 如何将游戏中 4 个静止帧的输入作为一个单一状态输入提供
- javascript - 如何使用带有自定义反应钩子的选项对象模式