错误:HTTP Status 500 - Dispatch[/LoginAction] to method in returned an exception--------------------------------------------------------------------------------type Status reportmessage Dispatch[/LoginAction] to method in returned an exceptiondescription The server encountered an internal error (Dispatch[/LoginAction] to method in returned an exception) that prevented it from fulfilling this request.
--------------------------------------------------------------------------------Apache Tomcat/5.0.28
--------------------------------------------------------------------------------Apache Tomcat/5.0.28
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.web.dao.UserInfoDAO" table="user_info"> <id name="userID" column="ID" type="long">
<generator class="identity"/>
</id>
<property name="userName" column="userName" type="string" not-null="true"/>
<property name="userPass" column="Password" type="string" not-null="true"/>
</class>
</hibernate-mapping>
UserInfoDAO.javapackage com.web.dao;
import java.io.Serializable;
public class UserInfoDAO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long userID;
private String userName;
private String userPass;
public UserInfoDAO() {
} public void setUserID(long userID) {
this.userID = userID;
}
public void setUserName(String userName) {
this.userName = userName;
} public void setUserPass(String userPass) {
this.userPass = userPass;
}
public long getUserID() {
return userID;
} public String getUserName() {
return userName;
} public String getUserPass() {
return userPass;
}
}帮忙看看,谢谢各位了
System.out.println(sql); // 打印出来了 From UserInfoDAO as a where a.userName='admin' and a.userPass='admin'query=s.createQuery(sql);System.out.println("end:"); //没有打印出来,应该在上句出错误了
import com.web.dao.UserInfoDAO;
import com.web.form.*;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;/**
* Implementation of <strong>Action</strong> that processes a user logoff.
*
* @author Craig R. McClanahan
* @version $Revision: 1.7 $ $Date: 2003/01/11 03:08:23 $
*/public final class LoginAction extends DispatchAction { public ActionForward in(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
LoginForm nb = (LoginForm) form;
HttpSession session = request.getSession();
BusinessManage bm = new BusinessManage();
UserInfoDAO user = new UserInfoDAO();
user.setUserName(nb.getUserName());
user.setUserPass(nb.getPassword());
System.out.println("Hibernate is Begin");
boolean flag = bm.checkUserInfo(user);
System.out.println("Hibernate is OK");
if (flag) {
session.setAttribute("LoginMessage", nb);
return (mapping.findForward("success"));
} else {
return (mapping.findForward("fault"));
}
} catch (Exception e) {
e.printStackTrace();
return (mapping.findForward("fault"));
}
} /*
* 退出系统
*/
public ActionForward out(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { HttpSession session = request.getSession();
session.removeAttribute("LoginMessage"); return (mapping.findForward("success"));
}}自己顶
2, 贴错误信息的时候,应该贴tomcat控制台上的错误信息,jsp上的这个错误信息对解决问题的帮助不是很大
import org.hibernate.cfg.*;
import com.web.dao.Link;
import com.web.dao.NoteBookDAO;
import com.web.dao.UserInfoDAO;public class BusinessManage
{
private SessionFactory sf = null;
private Session s = null;
private Transaction ts = null;
private Query query = null;
public static int PAGECOUNT;
public BusinessManage()
{
sf = new Configuration().configure().buildSessionFactory();
s = sf.openSession();
ts = s.beginTransaction();
} public void openSession()
{
s = sf.openSession();
}
/*
* 检验用户身份 Object: UserInfoDAO
*/
public boolean checkUserInfo(UserInfoDAO user) {
boolean flag = false;
try {
String sql="From UserInfoDAO as a where a.userName='"
+ user.getUserName() + "' and a.userPass='"
+ user.getUserPass() + "'";
System.out.println(sql);
query=s.createQuery(sql);
System.out.println("end:");
List lt=query.list();
if(lt.size()>0)
{
flag=true;
}
else
{
flag=false;
}
}
catch (Exception e) {
e.printStackTrace();
flag = false;
return flag;
}
return flag;
}
public void close()
{
s.close();
}
}
控制台没有报错误信息
只是打印了:
Hibernate is Begin
From UserInfoDAO as a where a.userName='admin' and a.userPass='admin'
不管怎样,在Session正常的情况下这一句报错只有一种情况: 那就是hql有错误, 可是hql现在没错
暂时不去想它,先提个建议:
1,断点调试,到了这一句的时候,看它是怎么个搞法,是蹦出错误了,还是卡住了,还是过了
2,操作Hibernate的方式很有问题,随便举个例子,在这个类中ts = s.beginTransaction();的ts一直没有commit, 每次new一个BusinessManage的时候,都要新建一个sessionFactory
楼主应该规范的方式来管理session,比如引入HibernateUtil, 把代码改了之后,看看还会halt不
1.应该是还是卡住了
2.这个代码的确是不规范,你说的也很有道理,我刚刚接触hibernate,ts的提交是在调用的方法手动写的,你说的每次new一个BusinessManage的时候,都要新建一个sessionFactory 这个的确是个大问题,我现在暂时不考虑性能问题,只是想让它正确执行,我还有个方法:
/*
* 插入用户留言Business 域模型-对象:NoteBookDAO
*
*/
public boolean insertNoteMessage(NoteBookDAO nb)
{
s.save(nb);
ts.commit();
return ts.wasCommitted();
}
是可以正确执行的插入到数据库中,不知道这个查询为什么就通不过
SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])说明session是存在的,sql应该也没错误啊,不知道为什么
上面有配置文件帮忙看看,郁闷
Hibernate对Session的管理要求是非常严格的
还是把代码改良吧,不要死钻牛角尖
规范的方式来管理session,比如引入HibernateUtil
具体应该怎么管理,修改代码
希望给点建议,谢谢
boolean flag = false;
Session s = null;
try {
s = HibernateUtil.getSession();
if(s!=null){
String sql="From UserInfoDAO as a where a.userName='"
+ user.getUserName() + "' and a.userPass='"
+ user.getUserPass() + "'";
System.out.println(sql);
query=s.createQuery(sql);
System.out.println("end:");
List lt=query.list();
if(lt.size()>0){
flag=true;
}
else{
flag=false;
}
}
}
catch (Exception e) {
e.printStackTrace();
flag = false;
return flag;
}
finally{
HibernateUtil.closeSession(s);
}
return flag;
}然后把BusinessManage中与session有关的以前的代码都砍掉
HibernateUtil这个类hibernate文档中有现成的,网上也一大把