【问题标题】:Unable to save entity using thymeleaf and Spring Boot无法使用 thymeleaf 和 Spring Boot 保存实体
【发布时间】:2018-01-13 16:08:37
【问题描述】:

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

我向我的项目实体添加了一个变量 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}",我不断收到“无法将类型“java.lang.String”的属性值转换为属性“用户”所需的类型“java.util.Set”。我不明白为什么 th:value="${user}" 应该是用户类时被视为字符串。有没有办法让我简单地获取选择的结果,循环遍历它并在控制器中手动将其添加到我的对象项目中?

我的控制器如下所示:

@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 spring spring-boot thymeleaf


    【解决方案1】:

    我能够修复“无法将类型“java.lang.String”的属性值转换为属性“用户”所需的类型“java.util.Set”。我只需要添加一个转换器类,告诉如何从字符串转换为用户对象。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 2017-05-11
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多