[英]Unable to save entity using thymeleaf and Spring Boot
我最近开始将Spring Boot(主要来自python / flask和node背景)与JPA和thymeleaf结合使用,并且正在尝试在数据库中创建一个Project。 一切进展顺利,我能够添加,删除等。项目。
我向项目实体添加了一个变量“设置用户”,如下所示:
@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "project_id")
private int id;
@Column(name = "title")
@NotEmpty(message = "*Please provide a title")
private String title;
@Column(name = "description")
private String description;
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "project_lead", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
private User projectLead;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "project_user", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
private Set<User> users;
...
}
用户类如下所示:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "user_id")
private int id;
@Column(name = "email")
@Email(message = "*Please provide a valid Email")
@NotEmpty(message = "*Please provide an email")
private String email;
@Column(name = "username")
@NotEmpty(message = "*Please provide a username")
private String username;
@Column(name = "password")
@Length(min = 5, message = "*Your password must have at least 5 characters")
@NotEmpty(message = "*Please provide your password")
@Transient
private String password;
@Column(name = "enabled")
private int enabled;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
...
}
当我不指定项目用户时,便可以创建新项目。 但是,当我使用多重选择指定用户时,创建项目时遇到了麻烦。 在我的表格中,我有:
<form th:action="@{/secure/projects}" th:object="${project}" method="POST">
<div class="form-group">
<label th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="validation-message"></label>
<input type="text" class="form-control" th:field="*{title}" id="title" th:placeholder="Title" />
</div>
<div class="form-group">
<input type="text" class="form-control" th:field="*{description}" id="description" th:placeholder="Description" />
</div>
<div class="form-group">
<select th:field="*{users}" class="users-select form-control" multiple="multiple">
<option th:each="user : ${allUsers}" th:value="${user}" th:text="${user.username}"></option>
</select>
</div>
<button name="Submit" value="Submit" type="Submit" th:text="Create"></button>
</form>
我不断收到“无法将th:field =“ * {users}”的属性'users'的类型'java.lang.String'的属性值转换为所需的类型'java.util.Set'。 我不明白为什么当th:value =“ $ {user}”应该是User类时才被认为是String。 有没有一种方法可以让我简单地获取选择的结果,循环进行选择,然后在控制器中手动将其添加到我的对象项目中?
我的控制器如下所示:
@RequestMapping(value = "/projects", method=RequestMethod.GET)
public ModelAndView showProjectForm() {
ModelAndView modelAndView = new ModelAndView();
// Get authenticated user
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findByEmail(auth.getName());
modelAndView.addObject("userName", "Welcome " + user.getUsername() + " (" + user.getEmail() + ")");
modelAndView.addObject("project", new Project());
modelAndView.addObject("allUsers", userService.findAll());
modelAndView.setViewName("project_creation");
return modelAndView;
}
@RequestMapping(value = "/projects", method=RequestMethod.POST)
public ModelAndView processProjectForm(@Valid Project project, BindingResult bindingResult) {
ModelAndView modelAndView = new ModelAndView();
// Get authenticated user
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findByEmail(auth.getName());
// Get all projects
List<Project> allProjects = projectService.findAll();
// Check if project already exists
Project projectExist = projectService.findByTitle(project.getTitle());
if(projectExist != null) {
bindingResult
.rejectValue("title", "error.project",
"There is already a project with this title");
}
// Get all users
List<User> allUsers = userService.findAll();
if(bindingResult.hasErrors()) {
modelAndView.addObject("userName", "Welcome " + user.getUsername() + " (" + user.getEmail() + ")");
modelAndView.addObject("project", new Project());
modelAndView.addObject("allUsers", allUsers);
modelAndView.setViewName("project_creation");
} else {
// Create project
project.setProjectLead(user);
projectService.saveProject(project);
modelAndView.addObject("userName", "Welcome " + user.getUsername() + " (" + user.getEmail() + ")");
modelAndView.addObject("success", "Project successfully created!");
modelAndView.addObject("project", new Project());
modelAndView.addObject("projects", allProjects);
modelAndView.setViewName("redirect:/secure/dashboard");
}
return modelAndView;
}
我能够将“用户”属性的“无法将类型'java.lang.String'的属性值转换为必需的类型'java.util.Set'的问题修复。 我只需要添加一个转换器类,告知如何从字符串转换为User对象。
@Component
public class StringToUser implements Converter<String, User> {
@Autowired
private UserService userService;
@Override
public User convert(String arg0) {
Integer id = new Integer(arg0);
return userService.findOne(id);
}
}
并将我表单中的选项更改为具有user.id的值,而不是用户对象。 转换器将负责其余的工作:
<option th:each="user : ${allUsers}" th:value="${user.getId()}" th:text="${user.getUsername()}"></option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.