0

错误

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());
}
...

我错过了什么?

4

1 回答 1

1

在您的initBinder方法中,您正在构建UserPropertyEditor自己,因为这使得它成为一个非弹簧管理的 bean,@Autowired它不会做任何事情。

要么您必须自己调用,要么您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);
    }
}

当您自己调用 setter 时,您必须将其注入UserService到您的控制器中,以便您可以访问它。现在构造一个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);
    }
}

使用时,ApplicationContexct您必须将其注入控制器,获取AutowireCapableBeanFactory并调用autowireBean新构造的 bean 实例上的方法。现在 Spring 将为您的对象执行任何注入和回调。

于 2014-06-12T06:27:42.937 回答