Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UserPropertyEditor中setAsText函数中的NullPointerException(Spring 3.2)_Spring_Jsp_Spring Mvc_Data Binding_Propertyeditor - Fatal编程技术网

UserPropertyEditor中setAsText函数中的NullPointerException(Spring 3.2)

UserPropertyEditor中setAsText函数中的NullPointerException(Spring 3.2),spring,jsp,spring-mvc,data-binding,propertyeditor,Spring,Jsp,Spring Mvc,Data Binding,Propertyeditor,错误 java.lang.NullPointerException at com.javalabs.web.dao.UserPropertyEditor.setAsText(UserPropertyEditor.java:20) ... 我无法从表单属性userResponsible中绑定参数,在该属性中,我获取要绑定到对象任务的用户id。 但我得到的参数是userResponsible=1,它是来自数据库的用户。有什么想法吗 form.jsp <%@ page language

错误

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

我遗漏了什么?

在您的
initBinder
方法中,您正在自己构造
UserPropertyEditor
,因为这使它成为一个非spring管理的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
并对新构造的bean实例调用
autowireBean
方法。现在Spring将为您的对象执行任何注入和回调操作。

用户服务不会被注入,因为它不是Spring管理的,您应该自己设置。您说在哪里?在用户属性编辑器中?我将尝试您所说的,但现在我不明白为什么其他属性编辑器类的状态、类别和优先级似乎工作正常?问题是没有userService类,因此存在空指针。其他编辑器可能没有任何其他自动连接的依赖项或依赖项。关键是如果没有
UserService