Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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
Java 无法自动连接MessageSource_Java_Spring_Spring Mvc_Model View Controller - Fatal编程技术网

Java 无法自动连接MessageSource

Java 无法自动连接MessageSource,java,spring,spring-mvc,model-view-controller,Java,Spring,Spring Mvc,Model View Controller,我有一个名为StudentWithAddress的班级,该班级将绑定,并在表格中填写值。我有一个自动连线的消息源。但是我在使用它时得到了NullPointerException web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSc

我有一个名为StudentWithAddress的班级,该班级将绑定,并在表格中填写值。我有一个自动连线的消息源。但是我在使用它时得到了NullPointerException

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>Spring Try App</display-name>
  <servlet>
    <servlet-name>front-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>front-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
ModelAtributeContinuedController.java:

package com.manya.spring.modelattribute;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/studentWithAddressAdmission")
public class ModelAttributeContinuedController {

    @InitBinder
    public void nameCustomeEditor(WebDataBinder binder)
    {
        binder.registerCustomEditor(String.class, "name", new CustomNameEditor());
    }

    @RequestMapping(value="/enterDetails", method=RequestMethod.GET)
    public ModelAndView admissionForm()
    {
        ModelAndView mv = new ModelAndView("StudentWithAddressClassDetails");
        return mv;
    }

    //the valid annotation tells spring that only when its auto data binding, it should put 
    //validation checks on the StudentWithAddress. if its not doing data binding, then the 
    //validation must not be done.
    @RequestMapping(value="/studentClassDetails" ,method=RequestMethod.POST)
    public ModelAndView details(@Valid @ModelAttribute("student1") StudentWithAddress student,BindingResult result)
    {
        //If any error was raised during data binding
        //the following code will be executed.
        if(result.hasErrors())
        {
            ModelAndView mv = new ModelAndView("StudentWithAddressClassDetails");
            return mv;
        }
        ModelAndView mv = new ModelAndView("StudentWithAddressClassDetailsSubmitted");
        student.usingMessageSourse();
        return mv;
    }


}
在ModelAttributeContinuedController.java的details()方法中,当我调用student.usingMessageSource()时,我得到以下错误

我尝试使用MessageSourceAware实现相同的功能,但也遇到了相同的异常


你调试过IDE中的代码了吗?您是否使用messagesourse()在
中设置了断点?请这样做(下次,在发布问题之前)并告诉我们您发现了什么。尝试将autowire=“byName”放在
中,它应该看起来像
autowire=“byName”>
我调试了应用程序,但它没有自动连接消息源。因此,当我调用usingMessageSource()时,它会给出NullPointerException@JimGarrisonI甚至尝试实现MessageSourceAware,但甚至没有调用setMessageSource回调方法。您在IDE中调试过代码吗?您是否使用messagesourse()在
中设置了断点?请这样做(下次,在发布问题之前)并告诉我们您发现了什么。尝试将autowire=“byName”放在
中,它应该看起来像
autowire=“byName”>
我调试了应用程序,但它没有自动连接消息源。因此,当我调用usingMessageSource()时,它会给出NullPointerException@JimGarrisonI甚至尝试实现MessageSourceAware,但甚至没有调用setMessageSource回调方法。
package com.manya.spring.modelattribute;

import java.util.Date;

import javax.validation.constraints.Size;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class StudentWithAddress {

    public StudentWithAddress()
    {
        System.out.println("from the constructor of the StudentWithAddress");
    }

    private String name;

    @Autowired
    private MessageSource messageSource;

    //This tells the spring the size constraint on the hobby field.
    @Size(min=2,max=15)
    private String hobby;

    private Date dob;
    private Address address;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    public Date getDob() {
        return dob;
    }
    public void setDob(Date dob) {
        this.dob = dob;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }

    public void usingMessageSourse()
    {
        System.out.println(messageSource.getMessage(name, null, null));
    }

}
package com.manya.spring.modelattribute;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/studentWithAddressAdmission")
public class ModelAttributeContinuedController {

    @InitBinder
    public void nameCustomeEditor(WebDataBinder binder)
    {
        binder.registerCustomEditor(String.class, "name", new CustomNameEditor());
    }

    @RequestMapping(value="/enterDetails", method=RequestMethod.GET)
    public ModelAndView admissionForm()
    {
        ModelAndView mv = new ModelAndView("StudentWithAddressClassDetails");
        return mv;
    }

    //the valid annotation tells spring that only when its auto data binding, it should put 
    //validation checks on the StudentWithAddress. if its not doing data binding, then the 
    //validation must not be done.
    @RequestMapping(value="/studentClassDetails" ,method=RequestMethod.POST)
    public ModelAndView details(@Valid @ModelAttribute("student1") StudentWithAddress student,BindingResult result)
    {
        //If any error was raised during data binding
        //the following code will be executed.
        if(result.hasErrors())
        {
            ModelAndView mv = new ModelAndView("StudentWithAddressClassDetails");
            return mv;
        }
        ModelAndView mv = new ModelAndView("StudentWithAddressClassDetailsSubmitted");
        student.usingMessageSourse();
        return mv;
    }


}
java.lang.NullPointerException
    at com.manya.spring.modelattribute.StudentWithAddress.usingMessageSourse(StudentWithAddress.java:61)
    at com.manya.spring.modelattribute.ModelAttributeContinuedController.details(ModelAttributeContinuedController.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)