问题描述
我正在尝试显示一个包含表单的模板,以便用户可以创建新主题.当我尝试显示页面时,出现以下错误
I am trying to display a template that will have a form so that a user can create a new subject. When I try to display the page I am getting the below errors
`java.lang.IllegalStateException: Neither BindingResult nor plain target
object for bean name 'subject' available as request attribute `
Servlet.service() for servlet [dispatcherServlet] in context with path []
threw exception [Request processing failed; nested exception is
org.thymeleaf.exceptions.TemplateInputException: An error happened during
template parsing (template: "class path resource
[templates/addSubject.html]")] with root cause
Error during execution of processor
'org.thymeleaf.spring4.processor.SpringInputGeneralFieldTagProcessor'
(template: "addSubject" - line 22, col 45)
我不知道这些错误是由这个模板还是我的其他类引起的.
I don't know if these errors are caused by this template or my other classes.
这是我添加的主题模板.
This is my add Subject template.
<form action="#" th:action="@{/addsubject}" th:object="${subject}"
method="post">
<table>
<tr>
<td> Subject Name:</td>
//this is line 22 where the error is <td><input id="subjectName" type="text" th:field="*{subjectName}" /></td>
<td th:if="${#fields.hasErrors('subjectName')}" th:errors="*{subjectName}">Surname error message</td>
</tr>
<tr>
<td> What is your grade goal for this subject? (e.g 50%,60%,70%) </td>
<td><inpurt type="double" th:fields="*{subjectGradeGoal}" /></td>
<td th:if="${#fields.hasErrors('subjectGradeGoal')}" th:errors="*{subjectGradeGoal}">subjectGradeGoal error message</td>
</tr>
<tr>
<td><button type="submt">Submit post</button></td>
</tr>
</table>
这是我的添加主题控制器,我在其中查找当前登录的用户并向其添加主题与主题具有一对多关系:
This is my Add subject Controller where I am finding the current logged in user and adding subject to it has a one to many relationship with subject:
@Controller
@RequestMapping("/addsubject")
public class AddSubjectController {
@Autowired
private SubjectRepository subjectRepository;
@Autowired
UserRepository userR;
@GetMapping
public Long currentUser(@ModelAttribute("userx") @Valid UserRegistrationDto userDto, BindingResult result, Model model) {
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String email = loggedInUser.getName();
User user = userR.findByEmailAddress(email);
String firstname = user.getFirstName();
String surname = user.getSurname();
Long userId = user.getUserId();
return userId;
}
public String addSubject(Subject subject) {
return "addSubject";
}
public String AddNewSubject(@Valid Subject subject, BindingResult bindingResult, Model model) {
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String email = loggedInUser.getName();
User user = userR.findByEmailAddress(email);
if(bindingResult.hasErrors()) {
return "addSubject";
}
subjectRepository.save(subject);
model.addAttribute("subjectName", subject.getSubjectName());
model.addAttribute("subjectGradeGoal", subject.getSubjectGradeGoal());
user.addSubject(subject);
return "userProfile1";
}
推荐答案
事实证明我的控制器没有任何问题.错误主要是由于我的 addSubject.html 和我的 userProfie.html 中的拼写错误造成的.
It turns out there wasn't anything wrong with my controller. The errors were mostly due to typos in my addSubject.html and in my userProfie.html.
这篇关于运行包含用于创建新实体的表单的模板时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!