gpt4 book ai didi

java - 无法使用 thymeleaf 和 Spring Boot 保存实体

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:13 26 4
gpt4 key购买 nike

我最近开始使用 Spring Boot(我主要来自 python/flask 和节点背景)以及 JPA 和 thymeleaf,并且我正在尝试在我的数据库中创建一个项目。一切都很顺利,我能够添加、删除等项目。

我向我的项目实体添加了一个变量 Set users,如下所示:

@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 类。有没有一种方法可以让我简单地获取选择的结果,循环遍历它并将其手动添加到 Controller 中到我的对象项目中?

我的 Controller 如下所示:

@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”的属性值转换为属性“users”所需的类型“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>

关于java - 无法使用 thymeleaf 和 Spring Boot 保存实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45528492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com