这是DaoImpl实现代码:package cn.edu.zzu.dao.impl;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;import cn.edu.zzu.dao.BasicDao;
import cn.edu.zzu.dao.interfaces.ILoginDao;
import cn.edu.zzu.entities.Student;public class LoginDaoImpl extends BasicDao implements ILoginDao { @Override
public Student findStudentBySname(Student stu) {
// TODO Auto-generated method stub
Criteria c = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Student.class);
c.add(Restrictions.eq("sname", stu.getSname()));
return (Student) c.uniqueResult();
}
}

解决方案 »

  1.   

    这是Action代码:
    import java.util.Map;import cn.edu.zzu.entities.Student;
    import cn.edu.zzu.services.interfaces.IStudentLogin;import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { /**
     * 
     */
    private static final long serialVersionUID = -1631064037091534904L;
        private IStudentLogin studentLogin;

    private Student stu;

    private String message;

    /**
     * dfsdfsdfsdfsdfsdfsdfsdfsdfsdf
     */
    @Override
    public String execute() throws Exception {
    try {
    Student stu2 = studentLogin.login(stu);
    if( stu2 == null ) {
    message = "您的用户名或密码错误!";
    return INPUT;
    }
    Map<String, Object> session = ActionContext.getContext().getSession();
    session.put("userinfo", stu2);
    } catch (Exception e) {
    System.out.println(e);
    }

    // TODO Auto-generated method stub
    return SUCCESS;
    } public Student getStu() {
    return stu;
    } public void setStu(Student stu) {
    this.stu = stu;
    }
    public void setStudentLogin(IStudentLogin studentLogin) {
    this.studentLogin = studentLogin;
    } public void setMessage(String message) {
    this.message = message;
    } public String getMessage() {
    return message;
    }}
      

  2.   

    从你断点来看,在action中Student stu2 = studentLogin.login(stu); 这句代码中的stu的属性sname=nullCriteria c = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Student.class);
    c.add(Restrictions.eq("sname", stu.getSname()));
    return (Student) c.uniqueResult();
    这段代码中不是要用sname匹配吗?既然你stu中的sname为null,所以找不到
      

  3.   

    楼上正解,你的sname为空,所以查不到
      

  4.   

    action里面的stu没有获取到值,可能是页面上的参数名和stu的参数名不对应的问题。
    尝试看一下stu.sname有没有值。也有可能是你的数据库里面sname没有设置唯一,导致返回的c.uniqueResult()报错了。。看下有没有抛出exception。