体会真正的Struts开发(2期)8

类别:Java 点击:0 评论:0 推荐:

下面我们编写生成的文件

 

首先LogonForm.java文件

public ActionErrors validate(

        ActionMapping mapping,

        HttpServletRequest request) {

        ActionErrors errors=super.validate(mapping,request);

        if(errors==null){

            errors=new ActionErrors();

        }

        if((password==null)||(password.trim().length()==0)){

            errors.add("password",new ActionError("password"));

        }

        if((userName==null)||(userName.trim().length()==0)){

            errors.add("userName",new ActionError("userName"));

        }

        return errors;

    }

修改Action类

public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response) {

        LogonForm logonForm = (LogonForm) form;

        Session s = null;

        try {

            s = YJBaseHBM.currentSession();

        } catch (HibernateException e) {

            e.printStackTrace();

            ActionErrors errors = new ActionErrors();

            errors.add("hbm.session.error",

                    new ActionError("hbm.session.error"));

            saveErrors(request, errors);

            return mapping.findForward("failure");

        }

        try {

            Query q=s.createQuery("from UserInfo  ui where ui.userName=:user and password=:password");

            q.setString("user", logonForm.getUserName());

            q.setString("password", logonForm.getPassword());

            System.out.println(q.toString());

            Iterator it = q.iterate();

            if(it.hasNext()==false){

                ActionErrors errors = new ActionErrors();

                errors.add("no.user",

                        new ActionError("no.user"));

                saveErrors(request, errors);

                return mapping.findForward("failure");

            }

            YJBaseHBM.closeSession();

        } catch (HibernateException e) {

            e.printStackTrace();

            ActionErrors errors = new ActionErrors();

            errors.add("hbm.session.query",

                    new ActionError("hbm.session.query"));

            saveErrors(request, errors);

            return mapping.findForward("failure");

        } catch (Exception e1){

            e1.printStackTrace();

            ActionErrors errors = new ActionErrors();

            errors.add("hbm.session.query",

                    new ActionError("hbm.session.query"));

            saveErrors(request, errors);

            return mapping.findForward("failure");

        }

       

        return mapping.findForward("success");

    }

logon.jsp与struts-config.xml配置修改见下载文件

本文地址:http://com.8s8s.com/it/it16694.htm