0
Good day! I try to create a new Task using form

  <fieldset >
    <form:hidden path="project" id="inputProjectID" class="form-control" value="${project.id}"/>
    <form:hidden path="id" id="inputID" class="form-control"/>
    <div class="form-group row">
        <form:label for="inputName" path="name" class="sr-only">Name</form:label>
        <form:input path="name" id="inputName" class="form-control" placeholder="Name"/>
     </div>
     <div class="form-group row">
        <form:label for="inputStoryPoints" path="storyPoints" class="sr-only">Description</form:label>
        <form:input path="storyPoints" id="inputStoryPoints" class="form-control" placeholder="Story Points"/>
     </div>
        <form:checkboxes path="skills" items="${skillsList}" id="choosingSkills"/>
     </fieldset>

</div>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    <input class="btn btn-primary" type="submit" value="Save"/>
  </div>
  </form:form>

这是模型

 @Entity
 @Table(name = "task")
 @JsonSerialize(using = TaskSerializer.class)
 public class Task implements Comparable<Task>{

@Id
@SequenceGenerator(name = "task_id_seq", sequenceName = "task_id_seq")
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "task_id_seq")
 @Column(name = "id")
private long id;

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;

@Column(name = "project_id", insertable = false, updatable = false)
long projectID;

@Column(name = "name")
private String name;

@Column(name = "story_points")
private int storyPoints;

@ManyToMany
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name = "skill_of_task",
            joinColumns={@JoinColumn(name = "task_id")},
            inverseJoinColumns={@JoinColumn(name = "skill_id")})
private List<Skill> skills;

//getters and setters
}

转换为所需对象

@InitBinder
  protected void initBinder(WebDataBinder binder)     {

        binder.registerCustomEditor(Skill.class,"skills", new PropertiesEditor() {  

            @Autowired
               private SkillDao skillDAO;  

          @Override
            public void setAsText(String text) {
                setValue(skillDAO.getSkillById(Long.valueOf(text)));
            }

          }
          );

}

但!我得到了例外 org.springframework.beans.MethodInvocationException: Property 'skills' throw exception; 嵌套异常是类 org.springframework.beans.AbstractNestablePropertyAccessor 的方法 setPropertyValue 中的 java.lang.NullPointerException。不幸的是,这是我能得到的所有信息。我累死。

4

1 回答 1

0

这个编辑器更好。SkillDAO 有一个空值,所以我把它移到了外部类中。

binder.registerCustomEditor(List.class, "skills", new CustomCollectionEditor(List.class)
      {  

          protected Object convertElement(Object element) {
                if (element != null) {
                    Long skillId = Long.parseLong(element.toString());
                    Skill skill = skillDAO.getSkillById(skillId );
                    return skill;
                }
                return null;
            }
      });
于 2018-02-04T12:50:16.340 回答