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)));
}
}
);
}
但是!我得到了exeg org.springframework.beans.MethodInvocationException:属性'技能'引发了异常;嵌套异常是类org.springframework.beans.AbstractNestablePropertyAccessor的方法setPropertyValue中的java.lang.NullPointerException。不幸 这是我能得到的所有信息。我累死。
答案 0 :(得分: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;
}
});