首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

SessionFactory为空,该怎么处理

2013-01-25 
SessionFactory为空问题如题,无法获取sessionFactory.控制台没有报错,浏览器报空指针异常.description The

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)
(省略)

项目结构:
SessionFactory为空,该怎么处理

/***************************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" alt="SessionFactory为空,该怎么处理" />
[解决办法]
SessionFactory为空,该怎么处理
[解决办法]
另外楼主以后要注意,Spring托管了所有的业务类。
不要再混淆了。

热点排行