的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebSLCM</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/resources/jsp/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/resources/jsp/404.jsp</location>
</error-page>
</web-app>
调度-servlet.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<mvc:annotation-driven />
<!-- <mvc:default-servlet-handler/> -->
<mvc:resources mapping="/resources/**" location="WEB-INF/resources/" />
<context:component-scan base-package="com.slv.controller" />
<!-- JDBC Properties -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/webslcm" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- Accounts -->
<bean id="accountsDaoImpl" class="com.slv.daoimpl.AccountsDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Hibernate Session properties -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.slv.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- ViewResolver Spring Will redirecting -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/resources/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Jsp Page:
<%!
@Autowired
public AccountsDaoImpl accountsDaoImpl;
%>
<%
List<Object[]> list = accountsDaoImpl.getSQLObjectValues("SELECT examId, examName FROM examtypes", session);
%>
当我在JSP中运行上面的代码时,我得到java.lang.NullPointerException
例外。
无法解决它,我错过了什么重要的事情吗?我应该对我的代码做些什么改变?
AccountsDaoImpl.java
public class AccountsDaoImpl implements AccountsDao{
@Autowired
public SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List<Object[]> getSQLObjectValues(String sqlQuery, HttpSession session2) {
Session session=sessionFactory.openSession(); // Here im getting the below exception
session.beginTransaction();
try{
Query query = session.createSQLQuery(sqlQuery);
List<Object[]> list=query.list();
return list;
}catch(Exception e){
e.printStackTrace();
}finally{
session.close();sessionFactory.close();
}
return null;
}
}
例外:
java.lang.NullPointerException
at com.slv.daoimpl.AccountsDaoImpl.getSQLObjectValues(AccountsDaoImpl.java:384)
at org.apache.jsp.WEB_002dINF.resources.jsp.reUsableReport_jsp._jspService(reUsableReport_jsp.java:179)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:3)
您没有使用Spring上下文为您生成bean,基本上忽略了所有Spring配置。您可以从JSP中的应用程序内容查看bean,但在JSP中编写应用程序代码总是一个坏主意!这就是为什么我不能给出更详细的答案,不要故意射击自己。在servlet中自动装配bean,并通过JSP中的请求调用它。
您可以使用SpringBeanAutowiringSupport
实用程序类来注入spring bean:
public class MyServlet implements HttpServlet {
@Autowired
private MyBean myBean;
public void init(ServletConfig config) {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this);
}
}
答案 1 :(得分:2)
要在JSP中启用自动连接,您必须在 SpringBeanAutowiringSupport 的帮助下通过覆盖 jspInit()函数启用它。
所以,修改后的 jsp Page 如下:
<%@ page import="org.springframework.beans.factory.annotation.Autowired"%>
<%@ page import="org.springframework.web.context.support.SpringBeanAutowiringSupport"%>
<%!
public void jspInit() {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,getServletConfig().getServletContext());
}
@Autowired
public AccountsDaoImpl accountsDaoImpl;
%>
<%
List<Object[]> list = accountsDaoImpl.getSQLObjectValues("SELECT examId, examName FROM examtypes", session);
%>
<强>调度-servlet.xml中:强>
您还必须为 AutowiredAnnotationBeanPostProcessor 添加bean
:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
所以,修改版本:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="WEB-INF/resources/" />
<context:component-scan base-package="com.slv.controller" />
<!-- Write Hibernate queries in JSP -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- JDBC Properties -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/webslcm" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- Accounts -->
<bean id="accountsDaoImpl" class="com.slv.daoimpl.AccountsDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Hibernate Session properties -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.slv.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- ViewResolver Spring Will redirecting -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/resources/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
<强>的web.xml:强>
您必须在 web.xml
context-param 和侦听器: org.springframework.web.context.ContextLoaderListener >修改:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebSLCM</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/resources/jsp/index.jsp</welcome-file>
</welcome-file-list>
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/resources/jsp/404.jsp</location>
</error-page>
</web-app>
答案 2 :(得分:1)
我认为最好在服务层中进行sql调用,并在控制器中填充表单支持对象,然后将对象列表发送回JSP以进行显示。是否需要在JSP中进行SQL调用?
答案 3 :(得分:0)
你不能这样做
AccountsDaoImpl accountsDaoImpl = new AccountsDaoImpl();
您需要从Spring上下文中获取accountsDaoImpl
@Autowired
public SessionFactory sessionFactory;
由Spring自动连线。
答案 4 :(得分:0)
我认为你应该使用sessionfactory的setter。
protected HibernateTemplate template = null;
/**
* Sets Hibernate session factory and creates a
* <code>HibernateTemplate</code> from it.
*/
public void setSessionFactory(SessionFactory sessionFactory) {
template = new HibernateTemplate(sessionFactory);
}
答案 5 :(得分:0)
我看到你有混合注释驱动和非注释驱动的样式。尝试:
从spring config xml
accountsDaoImpl
bean定义
从setSessionFactory
中移除setter方法AccountsDaoImpl
或将自动装配的注释从sessionFactory
实例变量移至setSessionFactory
方法。我认为要么会做,但你有两个都会导致你的问题。