简体   繁体   English

尝试检索Spring bean,但不断获取NullPointerException

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

I am trying to retrieve a BasicDataSource from a bean. 我正在尝试从bean检索BasicDataSource。 Whenever I try to access the bean, I get a NullPointerException 每当我尝试访问Bean时,我都会收到NullPointerException

My code: 我的代码:

<%@ 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>

My servlet context: 我的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>

No matter what, I keep getting a NullPointerException. 无论如何,我一直收到NullPointerException。 What am I doing wrong? 我究竟做错了什么?

There is nothing in your question to indicate that you have put an attribute named dataSource in the ServletContext used here 您的问题中没有任何内容表明您已在此处使用的ServletContext放置了一个名为dataSource的属性。

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

You seem to be confused by the naming of the Servlet API type ServletContext and the Spring WebApplicationContext loaded by the DispatcherServlet , also commonly referred to as the (dispatcher) servlet context. 似乎对Servlet API类型ServletContextDispatcherServlet加载的Spring WebApplicationContext的命名感到困惑, WebApplicationContext通常也称为(dispatcher)Servlet上下文。 Those are two completely different components. 这是两个完全不同的组件。

Here's some related content: 以下是一些相关内容:

You're probably better off using model attributes (request attributes). 使用模型属性(请求属性)可能更好。 To access a Spring bean from a JSP, you'll need to access the root WebApplicationContext or the servlet WebApplicationContext from the ServletContext using the appropriate attribute name, which may change from one version of Spring to the next, and access the bean on that. 要从JSP访问Spring bean,您需要使用适当的属性名从ServletContext访问根WebApplicationContext或Servlet WebApplicationContext ,这些属性名称可能会从Spring的一个版本更改为下一版本,并在该版本上访问bean。

In the view resolver configuration you can add the list of beans that you need to be available in JSP: 在视图解析器配置中,您可以添加需要在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.

相关问题 尝试使用 dynamodb 和 graphql 运行我的 Spring Boot 应用程序时,我不断收到此 @bean 错误 - I keep getting this @bean error when trying to run my spring boot app with dynamodb and graphql NullPointerException 尝试检索 @ManyToOne 父字段 - Spring Boot - NullPointerException trying retrieve @ManyToOne Parent Field - Spring Boot 尝试在jar中检索Spring bean文件时出现FileNotFoundException - FileNotFoundException when trying to retrieve spring bean file within jar 尝试将文件写入内部数据时不断获取NullPointerException - Keep getting NullPointerException when trying to write file to internal data 不断获取nullpointerException? 我正在尝试打印图像 - keep getting the nullpointerException? i am trying to print out an image 使用 Spring JPA 派生查询继续获取 nullpointerexception - Keep getting nullpointerexception using Spring JPA derived query 使用Spring注入bean时会出现nullPointerException - nullPointerException when injecting bean with Spring Spring Bean 给出 NullPointerException 但不为 null - Spring Bean gives NullPointerException but is not null 在 Spring 中创建 bean 期间的 NullPointerException - NullPointerException during bean creation in Spring 成功注入Spring bean的NullPointerException - NullPointerException on a Spring bean successfully injected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM