日期:2014-05-18  浏览次数:20758 次

一个困扰我1个星期的struts返回errors的问题。
先看我的程序:
JSP:
HTML code

<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic" %>

<html:form action="/logon.do"  >
<center>
<html:errors  />
<table border="0" width="100%">

  <tr>
    <th align="right">
      <bean:message key="prompt.username"/>
    </th>
    <td align="left">
      <html:text property="userName" size="15" maxlength="15"/>
    </td>
  </tr>

  <tr>
    <th align="right">
      <bean:message key="prompt.password"/>
    </th>
    <td align="left">
      <html:password property="password" size="15" maxlength="15"
                    redisplay="false"/>
    </td>
  </tr>

  <tr>
    <td align="right">
    <html:submit property="submit" >                     
        <bean:message key="button.logon"/>
    </html:submit>

    </td>
    <td align="left">
    <html:reset >                     
        <bean:message key="button.reset"/>
    </html:reset>

    </td>
  </tr>

</table>
</center>
</html:form>


ActionForm:
Java code

package forms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;


public final class LogonForm extends ActionForm {
    private String username = null;
    private String password = null;


    public String getUserName() {
        return (this.username);
    }

    public void setUserName(String UserName) {
        this.username = UserName;
    }


    public String getPassword() {
        return (this.password);
    }

    public void setPassword(String Password) {
        this.password = Password;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.username = null;
        this.password = null;

    }

    public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
        
        ActionErrors errors  = new ActionErrors();
        
        if((username == null)||(username.length()<1))
            errors.add("username",new ActionMessage("hello.no.username.error"));
        if((password == null)||(password.length()<1))
            errors.add("password",new ActionMessage("hello.no.password.error"));
        
        return errors;
    }
}


struts-config.xml
HTML code

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
       
<struts-config>
<!-- ============ Data Source =================================== -->

  <!-- ========== Form Bean Definitions =================================== -->
  <form-beans>
    [b]<form-bean      name="logonForm"
                    type="forms.LogonForm"/>[/b]
    <form-bean      name=