---
title: "体会真正的Struts开发(2期)8"
author: "fanjava"
date: "2004-06-27T19:03:00Z"
tags: []
slug: "struts-2-8"
draft: false
lastmod: "2004-06-27T19:03:00Z"
id: 601987
url: "https://fanjava.iteye.com/blog/601987"
---

下面我们编写生成的文件&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;

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