运行包含用于创建新实体的表单的模板时出错

时间:2018-02-07 14:50:25

标签: spring templates spring-boot thymeleaf

我正在尝试显示一个具有表单的模板,以便用户可以创建新主题。当我尝试显示页面时,我收到以下错误

  `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)  

我不知道这些错误是由此模板还是其他类引起的。

这是我的添加主题模板。

<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>

这是我的添加主题控制器,我找到当前登录的用户并添加主题与主题有一对多的关系:

@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";
}

2 个答案:

答案 0 :(得分:1)

对不起,迟到了。您可以尝试使用以下内容替换addSubject:

public String addSubject(Model model) {
    model.addAttribute("subject", new Subject());
    return "addSubject";
}

我认为百里香在模型属性中寻找“主题”而没有找到它。

其他方法也是如此,而不是model.addAttribute("subjectName", subject.getSubjectName());model.addAttribute("subjectGradeGoal", subject.getSubjectGradeGoal());您需要做model.addAttribute("subject", subject);

答案 1 :(得分:0)

事实证明我的控制器没有任何问题。错误主要是由于我的addSubject.html和我的userProfie.html中的拼写错误。