错误
java.lang.NullPointerException
at com.javalabs.web.dao.UserPropertyEditor.setAsText(UserPropertyEditor.java:20)
...
我无法从表单属性userResponsible中绑定参数,我在其中获取要绑定到对象 Task 的用户 ID。但是我得到了参数 userResponsible = 1,它是来自数据库的用户。有什么想法可以吗?
表单.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<h1>Create task</h1>
<form:form method="GET" class="form-horizontal"
action="${pageContext.request.contextPath}/docreatetask"
commandName="task">
...
<div class="form-group">
<label for="userresponsible" class="col-sm-2 control-label">User
responsible</label>
<div class="col-sm-10">
<form:select path="userResponsible" name="userResponsible" class="form-control">
<form:option value="0" label="Select" />
<form:options items="${users}" itemValue="idUser"
itemLabel="username" />
</form:select>
<div id="state.error">
<span class="text-danger"><form:errors path="userResponsible" /></span>
</div>
</div>
</div>
...
任务.java
@Entity
@Table(name = "t_task")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idTask")
private long idTask;
...
@ManyToOne
@JoinColumn(name = "idUser_responsible", nullable = true)
private User userResponsible;
...
用户属性编辑器.java
public class UserPropertyEditor extends PropertyEditorSupport {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
line 20: super.setValue(userService.get(Long.parseLong(text)));
//text variable is null when this function is called
}
}
任务控制器.java
...
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest req) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
binder.registerCustomEditor(State.class, new StatePropertyEditor());
binder.registerCustomEditor(Category.class, new CategoryPropertyEditor());
binder.registerCustomEditor(Priority.class, new PriorityPropertyEditor());
binder.registerCustomEditor(User.class, "userResponsible", new UserPropertyEditor());
//binder.registerCustomEditor(User.class, new UserPropertyEditor());
}
...
我错过了什么?