//------------------LogonForm-----------------package addressbook.forms;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
 * <strong>LogonForm</strong> handles the form
 * that the user will use to logon to the application.
 */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("error.username.required"));
            {
             errors.add("username", new ActionMessage("com.jeffaple.struts"));
             System.out.println("username can not be null!!");
            }
        if ((password == null) || (password.length() < 1))
            errors.add("password", new ActionMessage("error.password.required"));        return errors;    }
}//---------------logon.jsp-----------------
<%@ include file="taglibs.jsp" %>
<%@ include file="header.jsp" %>
<%@ include file="logonContent.jsp" %>
<%@ include file="footer.jsp" %>//---------------logonContent.jsp------------
<%@ include file="taglibs.jsp" %><html:errors/>
<html:form action="/logon.do" focus="userName" >
<center>
<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>

解决方案 »

  1.   

    //---------------------struts-config.xml-------------
    <?xml version="1.0" encoding="ISO-8859-1" ?><!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>
        <form-bean      name="logonForm"
                        type="addressbook.forms.LogonForm"/>
        <form-bean      name="searchForm"
                        type="addressbook.forms.SearchForm"/>                    
        <form-bean      name="insertForm"
                        type="addressbook.forms.InsertForm"/>
       
      </form-beans> 
      <!-- ========== Global Forward Definitions ============================== -->
      <global-forwards>
        <forward   name="logoff"               path="/logoff.do"/>
        <forward   name="logon"                path="/logon.jsp"/>
        <forward   name="success"              path="/mainMenu.jsp"/>
        <forward   name="search"               path="/search.jsp"/>
        <forward   name="displayall"           path="/displayall.do"/>
        <forward   name="insert"               path="/insert.jsp"/>
        <forward   name="mainMenu"             path="/mainMenu.jsp"/>
        <forward   name="confirmation"    path="/confirmation.jsp"/>
      </global-forwards>
      <!-- ========== Action Mapping Definitions ============================== -->
        <action-mappings>
       <action    path="/search"
                  type="addressbook.actions.SearchAction"
                  name="searchForm"
                  attribute="myForm"
                  scope="request"
                  input="/search.jsp">
          <forward name="success" path="/display.jsp"/>
        </action>
        <action    path="/displayall"
                   type="addressbook.actions.DisplayAllAction"
                   name="nestedForm"
                   scope="request"
                   input="/mainMenu.jsp">
          <forward name="success" path="/display.jsp"/>
        </action>
        
      
        <action   path="/insert"
                  type="addressbook.actions.InsertAction"
                  name="insertForm"
                  scope="request"
                  input="/insert.jsp"
                  validate="true">
     
        </action>
        
        <!-- Process a user logoff -->
        <action    path="/logoff"
                   type="addressbook.actions.LogoffAction">
          <forward name="success" path="/index.jsp"/>
        </action>    <!-- Process a user logon -->
        <action    path="/logon"
                   type="addressbook.actions.LogonAction"
                   name="logonForm"
                  scope="request"
                  input="/logon.jsp">
        </action>
        </action-mappings>
     
      <message-resources parameter="addressbook.ApplicationResources"/></struts-config>//-----------------LogonAction.java--------------------------
    package addressbook.actions;
    import java.io.IOException;
    import java.util.Hashtable;
    import java.util.Locale;
    import java.util.Properties;
    import java.util.ResourceBundle;
    import java.util.MissingResourceException;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionServlet;
    import org.apache.struts.util.MessageResources;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;import addressbook.Constants;
    import addressbook.model.UserBean;
    import addressbook.forms.LogonForm;/**
     * <strong>LogonAction</strong> provides the login mechanism
     * for the sample application. The username/password pair that
     * is entered by the user will be compared against the database
     * values that where read in at application startup. An error will
     * be reported if the username/password don't match, otherwise
     * a <code>User</code> Object will be inserted into the session
     * to indicate that the user is currently logged in.
     */
    public final class LogonAction extends AbstActionBase {
    private Log log =
            LogFactory.getLog(this.getClass().getName());
    public ActionForward execute(ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
    throws Exception { String userName=null;
    String password=null;
    Locale locale = getLocale(request);
    MessageResources messages = getResources(request); UserBean user = null;
    // Demonstrate a trace call to the logger
    if (log.isTraceEnabled()) {
    log.trace("LogonAction: entering method");
    }
    // Validate the request parameters specified by the user
    ActionMessages errors = new ActionMessages();
    if (form != null){
      userName = ((LogonForm) form).getUserName();
      password = ((LogonForm) form).getPassword();
    }
    Hashtable database = (Hashtable)
      servlet.getServletContext().getAttribute(Constants.DATABASE_KEY);
    if (database == null)
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                           new ActionMessage("error.database.missing"));
    else {
        user = (UserBean) database.get(userName);
        if ((user != null) && !user.getPassword().equals(password)){
    user = null;
    }
        if (user == null)
                    errors.add(ActionMessages.GLOBAL_MESSAGE,
                               new ActionMessage("error.password.mismatch"));
    } // Report any errors we have discovered back to the original form
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        return (new ActionForward(mapping.getInput()));
    }
    // Save our logged-in user in the session
    HttpSession session = request.getSession();
    session.setAttribute(Constants.USER_KEY, user);
    // Demonstrate a debug call to the logger
    if (log.isDebugEnabled()) {
    log.debug("LogonAction: User '" + user.getUserName() +
           "' logged on in session " + session.getId());
    }
            // Remove the obsolete form bean
    if (mapping.getAttribute() != null) {
                if ("request".equals(mapping.getScope()))
                    request.removeAttribute(mapping.getAttribute());
                else
                    session.removeAttribute(mapping.getAttribute());
            } // Forward control to the specified success URI
    return (mapping.findForward(Constants.FORWARD_SUCCESS));    }}
      

  2.   

    <html:errors/>
    显示所有的错误信息.
    如果要一条一条显示,用
    <html:errors property=""/>
      

  3.   

    <html:errors/>默认的是显示request或session范围内的所有ActionMessages或ActionErrors对象内容,<html:errors property="aa"/>只显示名为aa的对象
      

  4.   

    to rickhunterchen(千山鸟飞绝):
    显示所有的错误信息.?那是什么时候把error.header和error.footer加进去的呢?
    在form里面 validate时候,只有 errors.add("username", new ActionMessage("error.username.required"));?
      

  5.   

    那是不是在哪里先add进去一些ActionMessage了呢?
      

  6.   

    error.header和error.footer
    是自动加进去的
    每一个显示error的地方都会包含这个。主要用来做error的特殊显示。
      

  7.   

    Struts+Spring+Hibernate QQ群:9967568
      

  8.   

    >偶就是想知道,在哪里加进去的?是在 Struts 的 <html:errors 标签的具体实现中加进去的。如果楼主有兴趣,可以下一份源代码看看,写得极复杂,看半天或许只能看出个大概。