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

时间:2017-08-06 04:19:53

标签: java spring spring-boot thymeleaf

我最近开始使用Spring Boot(我主要来自python / flask和节点背景)与JPA和thymeleaf,我试图在我的数据库中创建一个项目。一切进展顺利,我能够添加,删除等项目。

我添加了一个变量Set users to my project entity,如下所示:

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

我不断变换&#39;无法转换类型&#39; java.lang.String&#39;的属性值要求的类型&#39; java.util.Set&#39;对于财产&#39;用户&#39;&#39;对于th:field =&#34; * {users}&#34;。我不明白为什么th:value =&#34; $ {user}&#34;当它被认为属于User类时,它被认为是一个String。有没有办法让我简单地获取select的结果,循环遍历它并在控制器中手动将它添加到我的对象项目中?

我的控制器看起来像这样:

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

1 个答案:

答案 0 :(得分:0)

我能够修复'无法将类型'java.lang.String'的属性值转换为属性'users''所需的类型'java.util.Set'。我只需添加一个转换器类,告诉如何从string转换为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>