我是第一次发帖,是 Spring 的初学者。我相信我已经掌握了基本概念,但是当我尝试编写自己的代码时遇到了一些错误。我做了功课,并查看了网站上的其他相关主题,但我仍然没有找到我的代码不起作用的原因。
如果我犯了任何愚蠢的错误,请原谅我。以下是我所有相关的代码片段:
DAO 接口
public interface TestDAO {
public int getId();
}
主 SQL 连接代码
import javax.sql.DataSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class TestDb extends JdbcDaoSupport implements TestDAO{
private SimpleJdbcTemplate jdbcTemplate;
public int getId()
{
String sql="SELECT DOI.DOI_ID FROM ***.DOI where DOI='test58'";
int id=jdbcTemplate.queryForInt(sql);
return id;
}
public void createTemplate(DataSource dataSource) {
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
}
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="webMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/Form.html">DoiEntry</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="DoiEntry" class="com.****.****.DoiEntry">
<property name="eventsService" ref="eventsService"/>
<property name="sendEventsQueueService" ref="sendEventsQueueService"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="**USER**" />
<property name="password" value="**PASSWORD**" />
</bean>
<bean id="testDAO" class="com.****.****.TestDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="testDb" class="com.****.****.TestDb">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
最后,我的 web.xml 文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>DOILookupApp</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
</description>
<servlet>
<servlet-name>DOILookup</servlet-name>
<servlet-class>com.****.****.DoiLookup</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DOILookup</servlet-name>
<url-pattern>/DOI</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>
functions.tld
</taglib-uri>
<taglib-location>
functions.tld
</taglib-location>
</taglib>
<!-- The main spring MVC servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
这是我用来在网页中查看的 JSP 文件。忽略 srciptlet,我知道它们是不好的编码习惯,我只需要一些东西来测试。
<%@ page language="java" import="java.util.*, java.sql.*;" %>
<html>
<head>
<title>Test DB</title>
</head>
<body>
<jsp:useBean id="testDb" scope="session" class="com.****.****.TestDb"/>
<%
int output=testDb.getId();
out.print("DOI ID for test58 is: ");
out.print(output);
%>
</body>
</html>
最后,这是我的错误信息
类型异常报告
信息
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
com.****.****.TestDb.getId(TestDb.java:15)
org.apache.jsp.jsp.testDb_jsp._jspService(testDb_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我知道要查看很多代码,但提前感谢您的所有帮助。
蒂姆