package org.apache.struts.webapp.example;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public final class LogonForm extends ActionForm {    private String password = null;
    private String username = null;    public String getPassword() {
return (this.password);
    }    public void setPassword(String password) {
        this.password = password;
    }    public String getUsername() {
return (this.username);
    }    public void setUsername(String username) {
        this.username = username;
    }    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.password = null;
        this.username = null;
    }
    /**
     * Validate the properties that have been set from this HTTP request,
     * and return an <code>ActionErrors</code> object that encapsulates any
     * validation errors that have been found.  If no errors are found, return
     * <code>null</code> or an <code>ActionErrors</code> object with no
     * recorded error messages.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if ((username == null) || (username.length() < 1))
            errors.add("username", new ActionError("error.username.required"));
        if ((password == null) || (password.length() < 1))
            errors.add("password", new ActionError("error.password.required"));
        return errors;
    }
}

解决方案 »

  1.   

    看看这个例子吧,它可以用来检验用户的输入Form是否正确,基本上可以用来代替客户端的javascript功能
      

  2.   

    那我在具体的程序中应该去如何去做呢,重写public ActionErrors validate(ActionMapping mapping,
                                     HttpServletRequest request)或public void reset(ActionMapping mapping, HttpServletRequest request)就可以了,struts什么时候调用呢》
      

  3.   

    教程
    http://www.chinajavaworld.net/non-cgi/usr/48/48_46.doc
    都在这里了
    http://www.chinajavaworld.net/forum/jinghua.cgi?action=list&forum=48
      

  4.   

    它用于保存页面上的值。
    确实每个页面都做一个formbean让人感觉受不了
      

  5.   

    看来很多人对于struts的工作流程不是很了解,建议看一些相关资料。
    struts1.1可以不用为每个表单都做一个forumbean了,可以在struts-config.xml里设置。
      

  6.   

    就是吗》》》太烦人!
    to ccjsmile() :
    怎么配置??给出格式吧