1

我正在使用 Spring STS(也使用 Eclipse-juno,spring 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");      
    }

控制器需要从 My Service Layer 界面执行检查方法(已标记):

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!!!");
    }
}

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

java.lang.NullPointerException 在 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”。

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

它在控制器<-> 服务层通信中。

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

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

第一步:在page_manager_service字段中添加@Autowired(删除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">

从根上下文.xml。

第四步:给Page_manager_service_mock_Impl添加@Component

4

1 回答 1

1

虽然你已经创建了Page_manager_servicebean,但是它并没有被注入到Controller类中,导致NPE你调用的时候

page_manager_service.check();

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

@Autowired
private Page_manager_service page_manager_service;

那么二传手将是不必要的......

旁注:Java 使用驼峰式大小写,这会使page_manager_service pageManagerService.

于 2013-02-25T18:15:18.023 回答