我是Spring的新手。我试图通过Thymeleaf形式创建一个新主题。用户和主题有一对多的关系。我想将创建的主题添加到当前用户。
当我尝试打开表单时,我收到以下错误:
BindingResult和bean名称' subject'都没有普通的目标对象。可用作请求属性
执行处理器' org.thymeleaf.spring4.processor.SpringInputGeneralFieldTagProcessor'时出错。 (模板:" addSubject" - 第22行,第45栏)
User Enity:(not the whole class)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
public List<Subject> subject;
public User() {
}
public User(Long userId, List<Subject> subject, Collection<Role> roles,
String username, String password,
String firstName, String surname, int age, double height, double
weight, String emailAddress, String gender,
String dob, Boolean studentStatus) {
super();
this.userId = userId;
this.subject = subject;
this.roles = roles;
this.username = username;
this.password = password;
this.firstName = firstName;
this.surname = surname;
this.age = age;
this.height = height;
this.weight = weight;
this.emailAddress = emailAddress;
this.gender = gender;
this.dob = dob;
this.studentStatus = studentStatus;
}
private String username;
private String password;
private String firstName;
private String surname;
private int age;
//@NotBlank
private String emailAddress;
//@NotBlank
private String gender;
//@NotBlank
private String dob;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public List<Subject> getSubject() {
return subject;
}
//Getters and setters are here
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// TODO Auto-generated method stub
return null;
}
public void addSubject(Subject subjects) {
subject.add(subjects);
}
添加主题控制器
@Controller
@RequestMapping("/addsubject")
public class AddSubjectController {
@Autowired
private SubjectRepository subjectRepository;
@ModelAttribute("subject")
public Subject subject() {
return new Subject();
}
@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(Model model) {
model.addAttribute("subject", new 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);
subjectRepository.save(subject);
model.addAttribute("subjectName", subject.getSubjectName());
model.addAttribute("subjectGradeGoal", subject.getSubjectGradeGoal());
model.addAttribute("subject", subject);
user.addSubject(subject);
return "userProfile1";
}
添加主题模板
添加主题
<h1>Add A Subject</h1>
<form action="#" th:action="@{/addsubject}" th:object="${subject}" method="post">
<table>
<tr>
<td> Subject Name:</td>
<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>
</form>
我不认为主题对象可以在模型中找到。