2 qq 30750507 qq_30750507 于 2016.05.10 16:08 提问

求各位大神帮忙struts2+hibernate 实现用户登录注册的代码 10C

控制台异常:严重: Exception occurred during processing request: Method "login" failed for object action.UsersAction@5d66d80b
ognl.MethodFailedException: Method "login" failed for object action.UsersAction@5d66d80b [java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration]
service层,usersdao代码:

          @Override
            public boolean usersRegist(Users u) {
        // TODO Auto-generated method stub
        //事务管理
        Transaction tx = null;
        Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
        try{
            tx = session.beginTransaction(); //开启事务 
            session.save(u);//将Users对象插入数据库 
            tx.commit();//提交事务
            return true;
        }
        catch(Exception ex){    
             ex.printStackTrace();//打印异常
             tx.commit();//提交事务
             return false;
        }finally{
            if(tx != null){
                tx = null;
            }
        }       
    }
    @Override
    public boolean usersLogin(Users u) {
        // TODO Auto-generated method stub
        Transaction tx = null;
        String hql="";
        Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
        try{
            tx = session.beginTransaction(); //开启事务 
            hql="from Users where username=? and password=?";
            Query query = session.createQuery(hql);
            query.setParameter(0,u.getUsername());
            query.setParameter(1, u.getPassword());
            List list = query.list();
            tx.commit();//提交事务
            if(list.size() > 0){
                return true;
            }else{
                return false;
            }

        }
        catch(Exception ex){    
             ex.printStackTrace();//打印异常

             tx.commit();//提交事务
             return false;
        }finally{
            if(tx != null){
                tx = null;
            }
        }
    }
    动作层:
    public class UsersAction extends SuperAction implements ModelDriven<Users>{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Users u = new Users();
    //用户注册动作
    public String regist(){
        UsersDAO udao = new UsersDAOImpl();
        if(udao.usersRegist(u)){            
            return "regist_success";
        }else{
            return "regist_failure";
        }
    }
    //用户登陆动作
    public String login(){
        UsersDAO udao = new UsersDAOImpl();
        if(udao.usersLogin(u)){         
            return "success";
        }else{
            return "failure";
        }
    }
    public Users getModel() {
        // TODO Auto-generated method stub
        return this.u;      
    }   
}
struts.xml配置:
<package name="users" namespace="/users" extends="default">
        <action name="*_*" class="action.{1}Action" method="{2}">
            <result name="regist_success">/users/Users_login_success.jsp</result>
            <result name="regist_failure">/users/Users_login.jsp</result>
            <result name="success">/users/Users_login_success.jsp</result>
            <result name="failure">/users/Users_login.jsp</result>          
        </action>
    </package> 

        .jsp表单:
        <form name="loginForm" action="<%=path%>/users/Users_login.action" method="post">
        <form name="registFrom" action="<%=path%>/users/Users_regist.action" method="post">

6个回答

CSDNXIAON
CSDNXIAON   2016.05.10 16:14

Struts2+hibernate实现用户登录和增删改查案例
Struts2+hibernate实现用户登录和增删改查案例
Struts2 拦截器实现用户登录(关键代码)
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

fubo1990
fubo1990   2016.05.10 16:16

错误说的是:你没有org/hibernate/cfg/Configuration配置文件,也就是你没有配置hibernate文件

u012377333
u012377333   Rxr 2016.05.10 16:21

Method "login" failed for object action.UsersAction@5d66d80b [java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration]

报错,找不到类报错;

你的逻辑显示,直接在DAO做的,也就是usersdao这个类,在你的Action响应层里面:

 public String login(){
        UsersDAO udao = new UsersDAOImpl();
        if(udao.usersLogin(u)){         
            return "success";
        }else{
            return "failure";
        }
    }

我猜测UsersDAOImpl这个是你usersdao类的接口类;
接口不是不能直接实例化对象的,直接
UsersDAO udao = new UsersDAO();
就可以了。

qq_30750507
qq_30750507 usersdao是业务逻辑接口类
一年多之前 回复
qq_30750507
qq_30750507   2016.05.10 16:21



root
root
com.mysql.jdbc.Driver
name="connection.url">jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8
<!-- 方言类, -->

org.hibernate.dialect.MySQLDialect
true
true
update
thread

    <mapping resource="entity/Users.hbm.xml"/>   <!--  将对象关系映射文档加入主配置 -->

</session-factory>


这是我配置的,对吗?我按着教程来的。


qq_30750507
qq_30750507   2016.05.10 16:22
 <hibernate-configuration>
    <session-factory >
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property 
        name="connection.url">jdbc:mysql:///test?useUnicode=true&amp;characterEncoding=UTF-8</property>
                <!-- 方言类, -->  
        <property name="dialect"> org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2dd1.auto">update</property>
        <property name="hibernate.current_session_context_class">thread</property>


        <mapping resource="entity/Users.hbm.xml"/>   <!--  将对象关系映射文档加入主配置 -->

    </session-factory>
</hibernate-configuration>

这个。发错了。

baidu_23086307
baidu_23086307   2016.05.10 16:25

楼上正解。。。。。。。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!