参考了别人的代码改的,出错了,帮忙看下啊各位业务逻辑
package mylibrary.BLL;import java.util.List;import mylibrary.Model.Admin;import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class AdminBll  extends HibernateDaoSupport { HibernateTemplate tmpl = null;

public AdminBll()
{
tmpl = getHibernateTemplate();
}

 
SessionFactory sessionFactory = new Configuration() .configure() .buildSessionFactory();

Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction(); 

public boolean login(Admin admin)
{  
String hql="FROM Admin admin WHERE  admin.username=? AND admin.password=?";
Query query=session.createQuery(hql);
query.setParameter(0,admin.getUsername());
query.setParameter(1,admin.getPassword());
List rs=query.list();
return rs!=null && rs.size()>0?true:false;

  //getHibernateTemplate().save(student);
  
 
}
}Actionpackage mylibrary.Action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.dispatcher.mapper.ActionMapping;import mylibrary.Model.Admin;import mylibrary.BLL.AdminBll;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{

public ActionForward loginAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
         Admin admin = (Admin) form;
         boolean flag = admin.login();
if(flag){
return mapping.findForward("success");
}else{
return mapping.findForward("fail");
}
}

}
ActionForward ,ActionForm 提示是出错了,找不到原因,求帮助!!

解决方案 »

  1.   

    Admin admin = (Admin) form;
    这能直接转的么,不是应该form.get()取值么
      

  2.   

    不太了解struts1  楼主用的是struts1?
      

  3.   


    首先,你的AdminBll.java,写的不太好,不跟你说什么设计模式的问题了。
    既然你只是初学。测试,就暂且这样。
    既然你这个类是继承自HibernateDaoSupport,那么,你就没有必要再去单独SessionFactory sessionFactory = new Configuration() .configure() .buildSessionFactory();
    获取sessionFactory,更没有必要通过
    Session session = sessionFactory.openSession();
    Transaction tr = session.beginTransaction();  
    获取session和事物。不然,你继承HibernateDaoSupport就白写了,没有一点实际作用。
    既然你继承了HibernateDaoSupport,直接给它注入它所需要的dataSource或者sessionFactory即可。不知道你是否集成了spring。你用了struts,那么,ActionForm form这个是跟画面绑定的。在action中,
    通过这句
    Admin admin = (Admin) form;是将ActionForm 直接转化为你的pojo类。
    Admin.java这个类应该是你的pojo类对应的po(区别于pojo实体影射类)。
     你怎么能用boolean flag = admin.login();这个来调你的方法呢?即使你没有dao,service层的概念,那也要这样写:
    AdminBll adminBll  = new AdminBll(); 
    boolean flag = adminBll.login(admin);
    从你的代码中可以看出,你对struts的原理不明白,对hibernate操作也不熟悉,对设计模式就更不清楚,对最基本的mvc更是无从谈起。打好基础,才能一步步走得更远。
      

  4.   

    没看到代码。看楼上有人说楼主你用的是struts1???