Search code examples
springtemplatesspring-bootthymeleaf

Error when running a template that contains a form to create a new entity


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

Solution

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