[英]NullPointerException in setAsText function in UserPropertyEditor (Spring 3.2)
ERROR 错误
java.lang.NullPointerException
at com.javalabs.web.dao.UserPropertyEditor.setAsText(UserPropertyEditor.java:20)
...
I can not bind the parameter from the form property userResponsible where I get the id of a user to bind to the object Task. 我无法从表单属性userResponsible绑定参数,在此我获取要绑定到对象Task的用户的ID。 But I'm getting the parameter userResponsible = 1, which is a user from database.
但是我得到参数userResponsible = 1,这是来自数据库的用户。 Any idea from what can be?
有什么想法吗?
form.jsp 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 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 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 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());
}
...
What am I missing? 我想念什么?
In your initBinder
method you are constructing the UserPropertyEditor
yourself, as this makes this a non spring managed bean the @Autowired
doesn't do a thing. 在您的
initBinder
方法中,您将自己构造UserPropertyEditor
,因为这使它成为非Spring托管的bean,@ @Autowired
不会执行任何操作。
Either you have to call the setUserService
yourself or you have to ask the application context to wire the instance for you. 您必须自己调用
setUserService
或者必须要求应用程序上下文为您连接实例。
@Controller
public class TaskController {
@Autowired
private UserService userService;
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest req) {
UserPropertyEditor upe = new UserPropertyEditor();
upe.setUserService(userService);
binder.registerCustomEditor(User.class, "userResponsible", upe);
}
}
When you call the setter yourself you have to inject the UserService
into your controller so that you have access to it. 当您自己调用设置器时,必须将
UserService
注入控制器中,以便可以访问它。 Now construct a UserPropertyEditor
and call the setUserService
method on it. 现在构造一个
UserPropertyEditor
并在其上调用setUserService
方法。
@Controller
public class TaskController {
@Autowired
private ApplicationContext context;
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest req) {
UserPropertyEditor upe = new UserPropertyEditor();
context.getAutowireCapableBeanFactory().autowireBean(upe);
binder.registerCustomEditor(User.class, "userResponsible", upe);
}
}
When using the ApplicationContexct
you have to inject that into your controller, get a AutowireCapableBeanFactory
and call the autowireBean
method on the freshly constructed instance of the bean. 使用
ApplicationContexct
,必须将其注入到控制器中,获得AutowireCapableBeanFactory
并在刚构造的bean实例上调用autowireBean
方法。 Now Spring will do any injection and callbacks for your object. 现在,Spring将为您的对象进行任何注入和回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.