`
yuanke
  • 浏览: 72904 次
  • 性别: Icon_minigender_2
  • 来自: 临沂
社区版块
存档分类
最新评论

getHibernateTemplate.get()返回null

    博客分类:
  • J2EE
阅读更多
[color=blue]Exception:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.NullPointerException
struts.action.LoginAction.execute(LoginAction.java:30)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.


这是spring配置文件:
<?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.0.xsd">

   <bean id="cdao" class="dao.CustomerDao2">
         <property name="sessionFactory">
               <ref local="sessionFactory"/>
         </property>
   </bean>
  
   <bean name="/login" class="struts.action.LoginAction">
          <property name="cdao">
                  <ref local="cdao"/>
          </property>
   </bean>
  
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="/WEB-INF/hibernate.cfg.xml">
</property>
</bean></beans>




这是hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">MySql</property>
<property name="show_sql">true</property>
<mapping resource="po/Customer.hbm.xml" />

</session-factory>

</hibernate-configuration>









这是struts.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="cunKuanForm" type="struts.form.CunKuanForm" />
    <form-bean name="loginForm" type="struts.form.LoginForm" />
    <form-bean name="regForm" type="struts.form.RegForm" />
    <form-bean name="quKuanForm" type="struts.form.QuKuanForm" />
   
 
  </form-beans>
 

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="cunKuanForm"
      input="/cunKuan.jsp"
      name="cunKuanForm"
      path="/cunKuan"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy" />
    <action
      attribute="loginForm"
      input="/login.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy" />
    <action
      attribute="regForm"
      input="/reg.jsp"
      name="regForm"
      path="/reg"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy" />
    <action
      attribute="quKuanForm"
      input="/quKuan.jsp"
      name="quKuanForm"
      path="/quKuan"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy" />
    <action path="/logout" type="struts.action.LogoutAction" />

  </action-mappings>
 
  <message-resources parameter="struts.ApplicationResources" />
 
   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
  </plug-in> 
 
</struts-config>



这是登陆LoginAction 的一部分:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import po.Customer;
import struts.form.LoginForm;
import dao.CustomerDao;
import dao.ICustomerDao;


public class LoginAction extends Action {
private ICustomerDao cdao;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
int account = loginForm.getAccount();
String password = loginForm.getPassword();

// Customer cus = new CustomerDao().getCustomerByAccount(account);
Customer cus = cdao.getCustomerByAccount(account);
if(cus==null || !cus.getPassword().equals(password)){//登陆不成功
         return new ActionForward("/error.jsp");
}
request.getSession().setAttribute("cus", cus);
request.getSession().setAttribute("admin", "admin");
return new ActionForward("/ope.jsp");

}
public ICustomerDao getCdao() {
return cdao;
}
public void setCdao(ICustomerDao cdao) {
this.cdao = cdao;
}
}





这是ICustomerDao 接口:
package dao;

import po.Customer;

public interface ICustomerDao {
   public abstract boolean insertCustomer(Customer cus);
  
   public abstract boolean updateCustomer(Customer cus);
  
   public abstract Customer getCustomerByAccount(int account);
  
}




这是hibernate 调用数据库的模板:
package dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import po.Customer;

public class CustomerDao2 extends HibernateDaoSupport implements ICustomerDao{


public Customer getCustomerByAccount(int account) {
return (Customer)this.getHibernateTemplate().get(Customer.class, account);
}

public boolean insertCustomer(Customer cus) {

return false;
}

public boolean updateCustomer(Customer cus) {
// TODO Auto-generated method stub
return false;
}

}


现在问题是用户一登陆,就报上面的异常,查了一天都没找出原因?
[/color]
分享到:
评论
1 楼 yuanke 2008-09-10  
dao如下:
public class UserDao{
HibernateTemplate ht;
SessionFactory sessionFactory ;
public void setSessionFactory(SessionFactory sessionFactory){
   this.ht=new HibernateTemplate(sessionFactory);
}
}

就ok了无需继承HibernateDaoSupport

相关推荐

    HibernateTemplate 的常规用法.doc

    return (Person) getHibernateTemplate().get(Person.class, new Integer(personId)); } } ``` 在这个例子中,`PersonDAOImpl`通过setter方法接收SessionFactory,并初始化HibernateTemplate。`getPersons()`方法...

    hibernateTemplate

    return (Person) getHibernateTemplate().get(Person.class, personId); } // 保存或更新Person实例 public void savePerson(Person person) { getHibernateTemplate().saveOrUpdate(person); } // 删除...

    HibernateTemplate的用法总结

    return (Person) getHibernateTemplate().get(Person.class, new Integer(personId)); } // 保存或更新Person public void saveOrUpdatePerson(Person person) { getHibernateTemplate().saveOrUpdate(person)...

    我的智囊团(SSH)01--07学习笔记

    return (Person) getHibernateTemplate().get(Person.class, new Integer(personId)); } public void savePerson(Person person) { getHibernateTemplate().saveOrUpdate(person); } // 其他类似的方法... }...

    spring_MVC源码

    02. `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 03. `name` varchar(45) NOT NULL, 04. `psw` varchar(45) NOT NULL, 05. PRIMARY KEY (`id`) 06.) 建好表后,生成实体类 [java] view ...

    HibernateDaoSupport与JdbcDaoSupport

    return null; } }); } } ``` - **HibernateDaoSupport**示例:`HibernateDaoSupport`同样支持编程式事务管理,但通常情况下会使用Hibernate自带的事务管理机制。 ```java public class TestA extends ...

    struts+spring+hibernate通用分页方法

    return (PageSupport) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = cq....

    ssh(structs,spring,hibernate)框架中的上传下载

    12. return (Tfile) getHibernateTemplate().get(Tfile.class, fileId); 13. } 14. public void save(Tfile tfile) 15. { 16. getHibernateTemplate().save(tfile); 17. getHibernateTemplate().flush(); 18. } 19. ...

    SSH常见面试题文库.pdf

    get() 方法一定要获取到真实的数据,否则返回 null。 5. Spring 事务 Spring 事务是指在 Spring 框架中对事务的支持。Spring 事务有多种方式,包括: * 声明事务 * 编程事务 Spring 事务的隔离级别包括: * ...

    (java web) jsp分页显示

    return ((Integer) list.get(0)).intValue(); } } ``` ##### 方法五:`getStartNum`方法 该方法用于计算每页的起始记录编号。 ```java public int getStartNum(int pageNum, int pageSize) { return (pageNum ...

    struts2购物车

    Buy goods = (Buy) getHibernateTemplate().get(Buy.class, id); return goods; } } ``` #### 五、总结 本案例展示了如何使用Struts2框架构建一个简单的购物车系统。通过前端页面与后端逻辑的紧密结合,实现...

    SSH面试总结(非常全)

    `load()`默认抛出`ObjectNotFoundException`异常,当实体不存在时,而`get()`方法则返回`null`。此外,`load()`仅在真正需要时才发出SQL查询,这称为懒加载,而`get()`方法则立即加载实体。 **7. Hibernate的缓存...

    HibernateDaoSupport分页.

    这些方法都通过调用`getHibernateTemplate().executeFind()`来执行具体的分页查询操作。 ##### 2. Dao接口定义 为了更好地组织代码,我们通常会定义一个DAO(Data Access Object)接口,用来规范数据访问层的行为...

    java程序员ssh面试常见题

    - **get()**:立即加载实体,如果实体不存在则返回`null`。 #### 7. Hibernate中lazy loading的概念及openSessionInView模式 - **知识点概述**:Lazy Loading是一种延迟加载策略,而OpenSessionInView模式则是在...

Global site tag (gtag.js) - Google Analytics