java - Trying to retrieve Spring bean but keep getting a NullPointerException -
i trying retrieve basicdatasource bean. whenever seek access 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 suscribing</title> </head> <body> <% seek { 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:
<?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 servlet's request-processing infrastructure --> <!-- enables spring mvc @controller programming model --> <annotation-driven /> <!-- handles http requests /resources/** efficiently serving static resources in ${webapproot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- resolves views selected rendering @controllers .jsp resources in /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, maintain getting nullpointerexception. doing wrong?
there nil in question indicate have set attribute named datasource
in servletcontext
used here
basicdatasource source = (basicdatasource)getservletcontext().getattribute("datasource");
you seem confused naming of servlet api type servletcontext
, spring webapplicationcontext
loaded dispatcherservlet
, commonly referred (dispatcher) servlet context. 2 different components.
here's related content:
difference between applicationcontext.xml , spring-servlet.xml in springyou're improve off using model attributes (request attributes). access spring bean jsp, you'll need access root webapplicationcontext
or servlet webapplicationcontext
servletcontext
using appropriate attribute name, may alter 1 version of spring next, , access bean on that.
java spring spring-mvc
No comments:
Post a Comment