ERROR的NullPointerException在setAsText功能UserPropertyEditor(春季3.2)
java.lang.NullPointerException
at com.javalabs.web.dao.UserPropertyEditor.setAsText(UserPropertyEditor.java:20)
...
我無法將參數從表單屬性綁定userResponsible哪裏獲得一個用戶的ID綁定到對象任務。 但我得到參數userResponsible = 1,這是一個來自數據庫的用戶。從什麼可能是什麼想法?
form.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>
...
Task.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;
...
UserPropertyEditor.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
}
}
TaskController.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());
}
...
我缺少什麼?
用戶服務不會被注入,因爲它不是一個管理的彈簧,您應該自己設置它。 –
你在說什麼?在用戶屬性編輯器中? – Joe