当前位置: 代码迷 >> J2EE >> getHibernateTemplate().find出现错误,请各位来看下是什么情况~~
  详细解决方案

getHibernateTemplate().find出现错误,请各位来看下是什么情况~~

热度:212   发布时间:2016-04-22 03:27:04.0
getHibernateTemplate().find出现异常,请各位大虾来看下是什么情况~~
一个小程序,在application中测试能完成,代码如下:

final ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
final SessionFactory sessionFactory = (SessionFactory)ctx.getBean("localsessionfactory");

public void nametest(){

HibernateTemplate template=new HibernateTemplate(this.sessionFactory);
EmployeeTableDAO employee = new EmployeeTableDAO();

EmployeeTable employeetable = new EmployeeTable();

employee.setHibernateTemplate(template);
if(employee.findByNameAndPass("hello", "hello").size()>0){
System.out.println("1");
System.out.println(employee.findByNameAndPass("hello", "hello").size());

}else{
System.out.println("2");
}
}

但是在web里面就有问题,代码如下:

 if(employee.findByNameAndPass("hello", "hello").size()>0){
// request.setAttribute("result" , "您已经成功登陆系统");
System.out.println("login success!");
return mapping.findForward("loginsuccess");

}else{
// request.setAttribute("result" , "不存在该用户或者用户名和密码不符");
System.out.println("login failed!");
return mapping.findForward("login");
}

调用的findByNameAndPass方法在EmployeeTableDAO类中,代码如下:
public List findByNameAndPass(String name, String pass) {
String[] args = {name,pass};
return getHibernateTemplate().find("from EmployeeTable where empName = ? and empPass = ?", args);
}

测试的application和web用的都是同一个applicationContext.xml配置文件,在web应用中也在struts-config.xml中进行了配置,配置文件如下:
 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" 
value="WEB-INF/applicationContext.xml" />
</plug-in>

现在的问题是同样的配置,同样的代码,在application里面可以通过,但在web里面就有问题,报错如下:
2008-8-24 10:50:29 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at cn.edu.aust.dao.EmployeeTableDAO.findByNameAndPass(EmployeeTableDAO.java:68)
at cn.edu.aust.struts.action.LoginAction.execute(LoginAction.java:56)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  相关解决方案