您的位置:首页 > 编程语言 > Java开发

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

2004-06-27 19:03 316 查看
  下面我们编写生成的文件<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
 
首先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配置修改见下载文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息