validate()函数好像没有错误。
请确认你的struts-config.xml的配置文件里,配置好了与使用哪个资源文件。

解决方案 »

  1.   

    用<html:error/>显示错误,这个没有问题,我用这个还显示了另外一个逻辑错误,没有错误,
    资源文件也没有问题.
      

  2.   

    哪个ActionErrors errors对象,是不是由框架自动转发到jsp页面?由<html:error/>接收.errors保存在那里?
      

  3.   

    ActionMessage error=new ActionMessage("hello.no.username.error");
    errors.add("username",error);
    用new ActionMessage ,不用new ActionError
      

  4.   

    楼上 正解用JBUILDER 那么写 就会报错
      

  5.   

    在jsp页面中,用<html;error/>显示错误,运行后,如果不输入任何东西,然后提交,应该显示"Please enter a<i>username</i>to say hello to!" 但我的程序提交后没有任何反应,这个问题困惑我好久了,不知到问题出在那?各位高手帮我看看到底错到那了?我的HelloForm.javapackage com.yourcompany.struts.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionError;
    //import org.apache.struts.action.ActionMessage; 
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;/** 
     * MyEclipse Struts
     * Creation date: 03-29-2006
     * 
     * XDoclet definition:
     * @struts.form name="helloForm"
     */
    public final class HelloForm extends ActionForm { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /**
     * 
     */

    /**
     * 
     */
         private static final long serialVersionUID = 1L;
    /** 
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    private String userName=null;
    public String getUserName(){
    return this.userName;
    }
    public void setUserName(String username){
    this.userName=username;
    }
    public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub
    this.userName=null;
     }
    public ActionErrors validate(
    ActionMapping mapping,
    HttpServletRequest request) { // TODO Auto-generated method stub
    ActionError error=new ActionError("hello.dont.take.to.monster");
    ActionErrors errors=new ActionErrors();
    // ActionMessage am=new ActionMessage("hello.no.username.error");
    if ((userName==null)||(userName.length()<-1)){
    errors.add("username",error);
    // errors.add("username",am);
    //return errors;
    }
    return errors;
    } /** 
     * Method reset
     * @param mapping
     * @param request
     */
    }我的jsp页面:<%@ page language="java"%>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
    <html:html locale="true"> 
    <head>
    <title><bean:message key="hello.jsp.title"/></title>
    <html:base/>
    </head>
    <body>

    hello ,everyone:test1
    <H2><bean:message key="hello.jsp.page.heading"/></H2><p>
    <html:errors property="username"/><p>

     <logic:present name="personbean" scope="request">
           <h2>
             <bean:message key="hello.jsp.page.hello"/>
            <bean:write name="personbean" property="userName"/><p>
            </h2>
         </logic:present>
        
    <html:form action="/hello.do" scope="userName">
        
         <bean:message key="hello.jsp.prompt.person"/>
     <html:text property="userName" size="16" maxlength="16"/> <br>
     <html:submit/><html:reset/>
    </html:form> 
       
    </body>
    </html:html>我的truts-config.xml文件:<form-beans >
        <form-bean name="helloForm" type="com.yourcompany.struts.form.HelloForm" />  </form-beans>  <global-exceptions />
      <global-forwards />
      <action-mappings >
        <action
          attribute="helloForm"
          input="/form/hello.jsp"
          name="helloForm"
          path="/hello"
          scope="request"
          type="com.yourcompany.struts.action.HelloAction" > 
          <forward name="SayHello" path="/form/hello.jsp"/>
          </action>
      </action-mappings>  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
    </struts-config>我的HelloAction.java:package com.yourcompany.struts.action;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionError;
    //import org.apache.struts.action.ActionMessage;
    import com.yourcompany.struts.form.HelloForm;/** 
     * MyEclipse Struts
     * Creation date: 03-29-2006
     * 
     * XDoclet definition:
     * @struts.action path="/hello" name="helloForm" input="/form/hello.jsp" scope="request" validate="true"
     */
    public class HelloAction extends Action { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) {
    HelloForm helloForm = (HelloForm) form;
    String username=helloForm.getUserName();
    String badUserName="monster";
    ActionError error=new ActionError("hello.dont.take.to.monster");
    ActionErrors errors=new ActionErrors();
    if((username.equalsIgnoreCase(badUserName))){
    // errors.add("username",new ActionMessage("hello.dont.take.to.monster"));
    errors.add("username",error);

    this.saveErrors(request, errors);
    return (new ActionForward(mapping.getInput()));
    //return (mapping.findForward("SayHello"));
    } // TODO Auto-generated method stub
    PersonBean pb=new PersonBean();
    pb.setUserName(username);
    pb.SaveToPersistentStore();
    request.setAttribute(Constants.PERSON_KEY, pb);

    return (mapping.findForward("SayHello"));
    }}
      

  6.   

    public ActionErrors validate(
    ActionMapping mapping,
    HttpServletRequest request) {
    ActionErrors errors=new ActionErrors();
    if ((userName==null)||(userName.length()<-1)){
    errors.add("userName",new ActionError("hello.no.username.error"));
    }
    return errors;
    }
    改成这样看看呢!
      

  7.   

    检查一下struts配置文件,有没有在相应的action设置加上validate=true
      

  8.   

    写JUnit test吧,一劳永逸。