CSDN博客

img arielxp

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

发表于2004/6/27 19:03:00  2237人阅读

分类: Struts

 

下面我们编写生成的文件

 

首先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配置修改见下载文件

阅读全文
0 0

相关文章推荐

img
取 消
img