簡體   English   中英

嘗試檢索Spring bean,但不斷獲取NullPointerException

[英]Trying to retrieve Spring bean but keep getting a NullPointerException

我正在嘗試從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。 我究竟做錯了什么?

您的問題中沒有任何內容表明您已在此處使用的ServletContext放置了一個名為dataSource的屬性。

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

似乎對Servlet API類型ServletContextDispatcherServlet加載的Spring WebApplicationContext的命名感到困惑, WebApplicationContext通常也稱為(dispatcher)Servlet上下文。 這是兩個完全不同的組件。

以下是一些相關內容:

使用模型屬性(請求屬性)可能更好。 要從JSP訪問Spring bean,您需要使用適當的屬性名從ServletContext訪問根WebApplicationContext或Servlet WebApplicationContext ,這些屬性名稱可能會從Spring的一個版本更改為下一版本,並在該版本上訪問bean。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM