试图检索Spring bean但继续得到NullPointerException

时间:2014-11-12 22:59:34

标签: java spring spring-mvc

我正在尝试从bean中检索BasicDataSource。每当我尝试访问bean时,我都会得到一个NullPointerException

我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.dbcp2.BasicDataSource" %>
<%@ page import="org.postgresql.*" %>
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Thank you for suscribing</title>
</head>
<body>
    <%  
        try {
            BasicDataSource source = (BasicDataSource)getServletContext().getAttribute("dataSource");
            out.write(source.toString());
        } catch(Exception e) {
            out.write(e.toString());
        }
    %>
    <p>${suscriber.name}<p/>
    <p>${suscriber.email}<p/>
</body>
</html>

我的servlet上下文:

<?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="com.edutainer4u.site" />



    <beans:bean id="dbURL" class="java.net.URI">
        <beans:constructor-arg
            value="'#{systemEnvironment['DATABASE_URL']}'">
        </beans:constructor-arg>
    </beans:bean>

    <beans:bean id="dataSource" 
        class="org.apache.commons.dbcp2.BasicDataSource">
        <beans:property name="url"
            value="jdbc:postgresql://localhost:10000/postgres">
        </beans:property>
        <beans:property value="blabla"
            name="username">
        </beans:property>
        <beans:property value="blabla"
            name="password">
        </beans:property>
    </beans:bean>
</beans:beans>

无论如何,我不断收到NullPointerException。我做错了什么?

2 个答案:

答案 0 :(得分:1)

您的问题中没有任何内容表明您在此处使用的dataSource中放置了名为ServletContext的属性

BasicDataSource source = (BasicDataSource)getServletContext().getAttribute("dataSource");

您似乎对Servlet API类型ServletContextDispatcherServlet加载的Spring WebApplicationContext的命名感到困惑,WebApplicationContext通常也称为(调度程序)servlet上下文。这是两个完全不同的组成部分。

以下是一些相关内容:

您最好使用模型属性(请求属性)。要从JSP访问Spring bean,您需要使用适当的属性名称从WebApplicationContext访问根ServletContext或servlet {{1}},该属性名称可能会从一个版本更改Spring到下一个,然后访问bean。

答案 1 :(得分:0)

在视图解析器配置中,您可以添加需要在JSP中可用的bean列表:

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="exposedContextBeanNames">
      <list>
          <value>dataSource</value>
      </list>
  </property>
</beans:bean>