尝试从我的控制器到达服务层时出现异常java.lang.NullPointerException

时间:2013-02-25 17:48:09

标签: eclipse spring spring-mvc

我正在使用Spring STS(也使用Eclipse-juno,第3.1版),并创建一个spring模板项目。

我的应用程序欢迎页面是一个简单的表格:

  <div id="editPresPage">
     <form action="editPresPage.do" method="post"> 
    <label>Enter Page Text</label><input type="text" name="page_text"/><br>
    <input type="submit" value="Add New Page"/>
 </form>              
  </div>

当我发送表格时,我的控制器到位:

@Controller
@RequestMapping(value = "/")
public class HomeController {

    private Page_manager_service page_manager_service;

    public void setPage_manager_service(Page_manager_service page_manager_service) {
        this.page_manager_service = page_manager_service;
    }

    @RequestMapping(value="/editPresPage",method = RequestMethod.POST)
    public ModelAndView EditPresPage()    {
        page_manager_service.check();

        return new ModelAndView("thanks");      
    }

控制器需要从我的服务层界面执行检查方法(已标记):

public interface Page_manager_service {
    public void check();
}

服务层实施:

public class Page_manager_service_mock_Impl implements Page_manager_service {

    public void check() {
        System.out.println("check method was done!!!");
    }
}

但是在执行检查方法时我会收到以下错误:

显示java.lang.NullPointerException     at my.topLevel.pack.HomeController.EditPresPage(HomeController.java:64)

这是我的servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring  beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="my.topLevel.pack" />

</beans:beans>

这是我的root-context.xml(你可以看到我连接了“page_manager_service”bean):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">    

    <!-- Root Context: defines shared resources visible to all other web components -->
    <context:annotation-config/> 

    <!--  Service Beans -->    
    <bean id="page_manager_service"  class="my.topLevel.pack.Services.Page_manager_service_mock_Impl">
    </bean> 
</beans> 

我没有更改web.xml文件中的任何内容。

我可以看到正在注入服务层实现,因为它上面有“s”。

我能够将参数从视图传输到控制器 - 所以问题就不存在了。

它在控制器中&lt; - &gt;服务层沟通。

我不知道为什么我会收到这个错误..

通过执行以下步骤解决了问题:

第一步:将@Autowired添加到page_manager_service字段(可以删除setter)。

第二步:在root-context.xml文件中更改:

<context:annotation-config/>

为:

<context:component-scan 
    base-package="my.topLevel.pack">
</context:component-scan> 

第三步:删除:

<bean id="page_manager_service"       class="my.topLevel.pack.Services.Page_manager_service_mock_Impl">

from root-context.xml。

第四步:将@Component添加到Page_manager_service_mock_Impl

1 个答案:

答案 0 :(得分:1)

虽然您已经创建了Page_manager_service bean,但它尚未注入Controller类,导致NPE调用

page_manager_service.check();

您可以使用Page_manager_service自动装配 @Component注释Controller

@Autowired
private Page_manager_service page_manager_service;

然后不需要设定者......

附注:Java使用camel-case来制作page_manager_service pageManagerService