|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在/MyFirstApplication/src/main/java/com/example/demo/app/inquiry/InquiryController.java代码如下
package com.example.demo.app.inquiry;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/inquiry")
public class InquiryController {
@GetMapping("/form")
public String form(InquiryForm inquiryForm, Model model) {
model.addAttribute("title", "Inquiry Form");
return "inquiry/form";
}
@PostMapping("/form")
public String formGoBack(InquiryForm inquiryForm, Model model) {
model.addAttribute("title", "Inquiry Form");
return "inquiry/form";
}
@PostMapping("/confirm")
public String confirm(@Validated InquiryForm inquiryForm,
BindingResult result,
Model model) {
if(result.hasErrors()) {
model.addAttribute("title", "Inquiry Form");
return "inquriy/form";
}
model.addAttribute("title", "Confirm Page");
return "inquriy/confirm";
}
}
/MyFirstApplication/src/main/resources/templates/inquiry/form.html中代码如下
<!--This is the completed HTML file -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}">Insert title here</title>
</head>
<body>
<h1 th:text="${title}">title</h1>
<form method="post" action="#" th:action="@{/inquiry/confirm}" th:object="${inquiryForm}">
<label for="name">Name:</label>
<input id="name" name="name" type="text" th:value="*{name}"><br>
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
<label for="email">Email:</label>
<input id="email" name="email" type="text" th:value="*{email}"><br>
<div th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
<label for="contents">Detail�@:</label>
<textarea name="contents" id="contents" rows="3" cols="40" th:field="*{contents}"></textarea><br>
<div th:if="${#fields.hasErrors('contents')}" th:errors="*{contents}"></div>
<input type="submit" value="Confirm">
</form>
</body>
</html>
/MyFirstApplication/src/main/resources/templates/inquiry/confirm.html中代码如下
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${title}">Insert title here</title>
</head>
<body>
<h1 th:text="${title}">无标题</h1>
<p>请确认输入内容</p>
<p th:text="${inquiryForm.name}"></p>
<div th:object="${inquiryForm}">
<p th:text="*{name}"></p>
<p th:text="*{email}"></p>
<p th:text="*{contents}"></p>
<form method="post" action="#" th:action="@{/inquiry/form}">
<input type="hidden" name="name" th:value="*{name}">
<input type="hidden" name="email" th:value="*{email}">
<input type="hidden" name="contents" th:value="*{contents}">
<input type="submit" value="Go Back">
</form>
<form method="post" action="#" th:action="@{/inquiry/complete}">
<input type="hidden" name="name" th:value="*{name}">
<input type="hidden" name="email" th:value="*{email}">
<input type="hidden" name="contents" th:value="*{contents}">
<input type="submit" value="Complete">
</form>
</div>
</body>
</html>
在浏览器打开http://localhost:8080/inquiry/form页面并且输入name email 和 contents后 点击confirm按钮后无法进入confirm页面 并出现以下报错
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [inquriy/confirm], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [inquriy/confirm], template might not exist or might not be accessible by any of the configured Template Resolvers
请问这是什么问题 我该怎么解决
这个问题是模板解析错误,即模板可能不存在或无法访问。根据您提供的代码和报错信息,可以看出是在确认页面的模板上出现了问题。
首先,请检查您的模板文件路径是否正确,确保"/MyFirstApplication/src/main/resources/templates/inquiry/confirm.html"这个路径下存在confirm.html模板文件。如果不存在,请创建该文件并确保命名正确。
另外,请注意模板文件名的大小写,确认文件名是否与代码中使用的模板名相匹配。在您的代码中,模板名应该是"confirm"而不是"Confirm",请将"return "inquriy/confirm";"修改为"return "inquiry/confirm";"。
最后,请确保您的IDE已经重新编译并重新启动了项目,以确保更改生效。
希望这些解决方法能够帮助您解决问题。如果问题仍然存在,请提供更多详细的错误信息,以便我能更好地帮助您解决。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|