【问题标题】:How do I access a custom bean in Spring MVC controller class?如何访问 Spring MVC 控制器类中的自定义 bean?
【发布时间】:2012-11-20 12:38:13
【问题描述】:

我是 Spring 新手,请多多包涵。

我正在查看本指南 (http://java2t.com/233/using-spring3-jdbctemplate-rowmapper-to-return-list-of-records/)。 它在 Eclipse 中使用“普通”java 项目。我正在尝试使用动态 Web 项目来模拟示例,但没有任何成功。

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID"
    version="3.0">

    <display-name>StaffDirectory</display-name>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
</web-app>

我的 spring-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.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">

    <context:component-scan base-package="org.flinders.staff.directory" />

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:view-controller path="/" view-name="index"  /> 

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="PVHmg57bE4" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

     <bean id="staffDirectoryDAO" class="org.flinders.staff.directory.dao.impl.StaffDirectoryDAOImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>

    <bean id="staffDirectoryService" class="org.flinders.staff.directory.services.impl.StaffDirectoryServiceImpl">
        <property name="staffDirectoryDAO" ref="staffDirectoryDAO" />
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" >
            <value>org.springframework.web.servlet.view.tiles2.TilesView</value>
        </property>
    </bean>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean> 
</beans>

我的控制器类

package org.flinders.staff.directory.controllers;

import java.util.List;

import org.flinders.staff.directory.models.database.StaffModel;
import org.flinders.staff.directory.models.misc.SearchModel;
import org.flinders.staff.directory.services.StaffDirectoryService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StaffDirectoryController {
    private StaffDirectoryService staffDirectoryService;

    @RequestMapping("/SearchResults")
    public void showSearchResults() {
        //StaffDirectoryService staffDirectoryService = (StaffDirectoryService) getServletContext().getBean("StaffDirectoryService");
        List<StaffModel> staffList = staffDirectoryService.viewStaffResults();

        for (StaffModel staffModel : staffList) {

          System.out.println(staffModel.getStaffID() + "    : "

              + staffModel.getFirstname() + "  : " + staffModel.getSurname());

        }

        System.out.println();

    }

    @RequestMapping("/SearchForm")
    public ModelAndView showSearchForm() {
        return new ModelAndView("search/SearchForm", "SearchModel", new SearchModel());
    }
}

我没有遇到任何发布/重新启动我的 Tomcat 实例的情况。但是去我的浏览器,我的代码 在我的控制器类中对此代码 (List staffList = staffDirectoryService.viewStaffResults();) 引发异常。

有什么想法吗?谢谢! :)

【问题讨论】:

标签: spring model-view-controller javabeans


【解决方案1】:

使用 Autowired Annotation 注入服务对象

首先...导入 Autowired Annotation

import org.springframework.beans.factory.annotation.Autowired;

并通过注解注入服务对象。

@Autowired
private StaffDirectoryService staffDirectoryService;

【讨论】:

    【解决方案2】:

    看起来你没有注入StaffDirectoryService,所以当你调用viewStaffResults()时它是空的,从而抛出一个NullPointerException

    添加 @Autowired 注释应该可以帮助您解决这个问题:

    @Autowired
    private StaffDirectoryService staffDirectoryService;
    

    【讨论】:

    • 我刚刚编辑了我的控制器类以包含 @Autowired 并收到此错误“Autowired cannot be resolve to a type”
    • 您使用的是什么版本的 Spring?至少 2.5?
    • 您是否手动将罐子放入您的 WEB-INF 中?你的 WEB-INF 中有 Spring 核心 jar 吗?
    • 我将所有需要的 JAR 复制到 WEB-INF 中的 lib 文件夹中,然后将它们添加到构建路径中。我正在使用 3.1.3 spring。
    • 我包含/加载的 JAR 的屏幕截图 = i1162.photobucket.com/albums/q535/mrjayviper/Untitled-1.jpg。不确定这是否重要,但我使用的是最新的 STS 版本:3.1.0.RELEASE 构建 ID:201210061306
    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多