SessionFactory为空
问题如题,无法获取sessionFactory.控制台没有报错,浏览器报空指针异常.
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
org.emper.xuanke.dao.impl.UserDaoImpl.validateUser(UserDaoImpl.java:15)
org.emper.xuanke.action.UserAction.execute(UserAction.java:61)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
(省略)
项目结构:
/***************************applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username" value="system"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/emper/xuanke/vo/Student.hbm.xml</value>
<value>
org/emper/xuanke/vo/Selectedcourse.hbm.xml
</value>
<value>
org/emper/xuanke/vo/Selectivecourse.hbm.xml
</value>
<value>org/emper/xuanke/vo/Usertable.hbm.xml</value></list>
</property>
</bean>
<bean id="userDaoImpl" class="org.emper.xuanke.dao.impl.UserDaoImpl" >
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<bean id="userAction" class="org.emper.xuanke.action.UserAction">
<property name="userDaoImpl">
<ref bean="userDaoImpl"/>
</property>
</bean>
</beans>
/***************************web.xml文件************/
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener >
<listener-class >
org.springframework.web.context.ContextLoaderListener
</listener-class >
</listener >
</web-app>
/***************************UserAction.java****************/
//struts.xml里面处理网页的post表单就是用这个action
package org.emper.xuanke.action;
import java.util.Map;
import org.emper.xuanke.dao.UserDaoIf;
import org.emper.xuanke.dao.impl.UserDaoImpl;
import org.emper.xuanke.vo.Usertable;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
protected String username;
protected String password;
protected String type;
protected String name;
protected Usertable usertable;
protected UserDaoImpl userDaoImpl;
public UserDaoImpl getUserDaoImpl() {
return userDaoImpl;
}
public void setUserDaoImpl(UserDaoImpl userDaoImpl) {
this.userDaoImpl = userDaoImpl;
}
public Usertable getUsertable() {
return usertable;
}
public void setUsertable(Usertable usertable) {
this.usertable = usertable;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception{
//用户登陆
System.out.println("什么情况?111");
UserDaoIf ud=new UserDaoImpl();
Usertable u=ud.validateUser(usertable.getUsername(),usertable.getPassword());
System.out.println("什么情况?22");
if(u !=null){
System.out.println("什么情况?33");
Map session=ActionContext.getContext().getSession();
session.put("user", u);
//Cart cart=new Cart();
//session.put("cart", cart);
System.out.println("什么情况?44");
return SUCCESS;
}
else{
Map session=ActionContext.getContext().getSession();
session.put("wrong","wrong");
return ERROR;
}
//return SUCCESS;
}
}
/*****************************struts.xml文件*************************/
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login" class="org.emper.xuanke.action.UserAction">
<result name="success">index.jsp</result>
<result name="error">eduAdmin.jsp</result>
</action>
</package>
</struts>
/***************************UserDaoImpl.java文件************/
package org.emper.xuanke.dao.impl;
import org.emper.xuanke.dao.UserDaoIf;
import org.emper.xuanke.vo.Usertable;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
public class UserDaoImpl implements UserDaoIf{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Usertable validateUser(String username,String password){
System.out.println("zhe里进去了111");
System.out.println("sessionfactory111:"+sessionFactory);//此处输出为空,也就是SessionFactory为空.后面的就无法进行了.
Session session=sessionFactory.openSession();
System.out.println("sessionfactory222:"+sessionFactory);
String hql="from Usertable ut where ut.username=? and ut.password=?";
Query query=session.createQuery(hql);
query.setParameter(1, username);
query.setParameter(2, password);
System.out.println("这里进不来222");
if(query.uniqueResult()!=null){
Usertable userT=(Usertable)query.uniqueResult();
return userT;
}else{
return null;
}
}
}
我认为需要的代码都贴出来了.弄了它两天了,没想出个所以然,求帮助.3q
[最优解释]
恩恩,LZ对的,我刚说着配置咋没错了。
弄的我自己对着项目的配置文件看了会,没错。
原来你是自己有重新new了一个对象出来,无法获取sessionFactory了。
LZ散分,呵呵!
[其他解释]
看起来楼主用的还是hibernate3。
等下我也分享下spring3+hibernate4+struts2的整合配置经验。spring3+hibernate4与spring3+hibernate3的配置在很多地方不一样。
[其他解释]
恭喜楼主。
[其他解释]
另外楼主以后要注意,Spring托管了所有的业务类。
不要再混淆了。
[其他解释]
null
[其他解释]
找到解决办法了..
UserDaoIf ud=new UserDaoImpl();
Usertable u=ud.validateUser(usertable.getUsername(),usertable.getPassword());
这两句改为:
Usertable u=userDaoIml.validateUser(usertable.getUsername(),usertable.getPassword());
原因是我这里重新初试化了一个新的UserDaoImpl类,而新初试化的是无法得到从spring注入的SessionFactory类的信息.
相关知识__http://blog.sina.com.cn/s/blog_68d733750100kc3i.html
[其他解释]
呵呵……楼主是个有趣的人。自己解决了问题,也要恭喜楼主了,呵呵。
[其他解释]
这问题缠了我两天了!!谁知道刚发贴求助后,30分钟后就找到问题所在了。。
当个经验吧,在此分享给网友们。
[其他解释]
null