You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dick Starr <di...@starrcs.com> on 2002/01/22 04:20:27 UTC

getting validation errors on initial display of a form

I am new to Struts and am using Ted's sample logon app as my basis for
learning. His part works fine. I added a new jsp page that contains two
fields, created a form bean & an action form with validation edits. My
problem is that when I execute my form via
http://localhost:8080/starrd/app/names.do I get validation errors appearing
immediately (before I enter any data in the form). I can't believe thats
normal: I must be doing something wrong.

Thanks in advance for the help.

Dick Starr

Here's the pertinent parts of my struts-config:

  <form-beans>
  <form-bean
    name="nameForm"
    type="com.starrcs.app.SANameForm"/>

  <global-forwards>
    <forward
      name="names"
      path="/app/names.do"/>

  <action-mappings>
    <action
      path="/app/names"
      type="com.starrcs.app.SANameAction"
      name="nameForm"
      scope="request"
      validate="true"
      input="/pages/app/Name.jsp">
      <forward
        name="continue"
        path="/pages/app/Name.jsp"/>
    </action>

Here's my Name.jsp:

<!-- Name.jsp 2002/01/21 RJS -->
<%@ page language="java" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html><head><title><bean:message key="app.name.title"/></title></head>
<html:base/>
<body>
<html:errors/>
<html:form action="/app/names" focus="name">
<table border="0" width="100%">
<tr><th align="right">Name:</th><td align="left">
  <html:text property="name"/></td>
</tr>
<tr><th align="right">Address line one:</th><td align="left">
  <html:text property="addr1"/></td>
<tr>
<td align="right"><html:submit property="submit" value="Submit"/></td>
<td align="left"><html:reset/></td>
</tr>
</table>
</html:form>
</body>
</html>

<%--

Allow user to submit username and password to logon action.

--%>

Here's the pertinent parts of SANameAction.java:

public final class SANameAction extends Action
{
  public ActionForward perform(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
        throws IOException, ServletException
  {
    // Obtain fields from web tier
    String name = ((SANameForm) form).getName();
    String addr1 = ((SANameForm) form).getAddr1();

    // Log this event, if appropriate
    if (servlet.getDebug() >= SAConstants.DEBUG)
    {
      StringBuffer message =
        new StringBuffer("SANameAction: name = ");
          message.append(name);
          message.append(", addr1 = ");
          message.append(addr1);
          servlet.log(message.toString());
    }

    // Forward control to the welcome URI
    // specified in the configuration.
    return (mapping.findForward(SAConstants.WELCOME));
  }

} // End SANameAction

Here's the pertinent parts of SANameForm.java:

public final class SANameForm extends ActionForm
{
  // ---- Instance Variables

  private String name = null;
  private String addr1 = null;

  // ---- Properties

  public String getName()
  {
    return (this.name);
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public String getAddr1()
  {
    return (this.addr1);
  }

  public void setAddr1(String addr1)
  {
    this.addr1 = addr1;
  }

// ---- Public Methods
    public void reset(ActionMapping mapping,
        HttpServletRequest request)
    {
      setName(null);
      setAddr1(null);
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request)
    {
      ActionErrors errors = new ActionErrors();
      if ((name == null) || (name.length() < 1))
        errors.add("name",
          new ActionError("app.error.name.required"));
      if ((addr1 == null) || (addr1.length() < 1))
        errors.add("addr1",
          new ActionError("app.error.addr1.required"));
      return errors;
    }

} // End SANameForm

When I first execute the form via  http://localhost:8080/starrd/app/names.do
I immediately (before entering any data or executing the submit button) I
get validation errors displayed for name and addr1 and get the following in
my log file:

2002-01-21 20:19:19 action: Processing a GET for /app/names
2002-01-21 20:19:19 action:  Looking for ActionForm bean under attribute
'nameForm'
2002-01-21 20:19:19 action:  Creating new ActionForm instance of class
'com.starrcs.app.SANameForm'
2002-01-21 20:19:19 action:  Storing instance under attribute 'nameForm' in
scope 'request'
2002-01-21 20:19:19 action:  Populating bean properties from this request
2002-01-21 20:19:19 action:  Validating input form properties
2002-01-21 20:19:19 action:   Validation error(s), redirecting to:
/pages/app/Name.jsp

Then if I fill in the two fields with data, I do not get validation errors
(which is correct). Then this is what is in my log:

2002-01-21 20:19:37 action: Processing a POST for /app/names
2002-01-21 20:19:37 action:  Looking for ActionForm bean under attribute
'nameForm'
2002-01-21 20:19:37 action:  Creating new ActionForm instance of class
'com.starrcs.app.SANameForm'
2002-01-21 20:19:37 action:  Storing instance under attribute 'nameForm' in
scope 'request'
2002-01-21 20:19:37 action:  Populating bean properties from this request
2002-01-21 20:19:37 action:  Validating input form properties
2002-01-21 20:19:37 action:   No errors detected, accepting input
2002-01-21 20:19:37 action:  Looking for Action instance for class
com.starrcs.app.SANameAction
2002-01-21 20:19:37 action: SANameAction: name = data1, addr1 = data2
2002-01-21 20:19:37 action: Processing a POST for /app/welcome
2002-01-21 20:19:37 action:  Looking for Action instance for class
com.starrcs.app.SAContinueAction



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: getting validation errors on initial display of a form

Posted by Anant Sagar <an...@rapideffect.com>.
Since you are calling "names.do"   directly, formbean calls validate() method before forwarding to appropriate action class and hence the erros are displayed.
Call your form using this  http://localhost:8080/starrd/pages/app/Name.jsp   initially  
and set action="/app/names" while  submitting. 
Hope that may help .

Anant Sagar

----- Original Message ----- 
From: "Dick Starr" <di...@starrcs.com>
To: "struts-user" <st...@jakarta.apache.org>
Sent: Tuesday, January 22, 2002 8:50 AM
Subject: getting validation errors on initial display of a form


> I am new to Struts and am using Ted's sample logon app as my basis for
> learning. His part works fine. I added a new jsp page that contains two
> fields, created a form bean & an action form with validation edits. My
> problem is that when I execute my form via
> http://localhost:8080/starrd/app/names.do I get validation errors appearing
> immediately (before I enter any data in the form). I can't believe thats
> normal: I must be doing something wrong.
> 
> Thanks in advance for the help.
> 
> Dick Starr
> 
> Here's the pertinent parts of my struts-config:
> 
>   <form-beans>
>   <form-bean
>     name="nameForm"
>     type="com.starrcs.app.SANameForm"/>
> 
>   <global-forwards>
>     <forward
>       name="names"
>       path="/app/names.do"/>
> 
>   <action-mappings>
>     <action
>       path="/app/names"
>       type="com.starrcs.app.SANameAction"
>       name="nameForm"
>       scope="request"
>       validate="true"
>       input="/pages/app/Name.jsp">
>       <forward
>         name="continue"
>         path="/pages/app/Name.jsp"/>
>     </action>
> 
> Here's my Name.jsp:
> 
> <!-- Name.jsp 2002/01/21 RJS -->
> <%@ page language="java" %>
> <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
> <%@ taglib uri="/tags/struts-html" prefix="html" %>
> <html><head><title><bean:message key="app.name.title"/></title></head>
> <html:base/>
> <body>
> <html:errors/>
> <html:form action="/app/names" focus="name">
> <table border="0" width="100%">
> <tr><th align="right">Name:</th><td align="left">
>   <html:text property="name"/></td>
> </tr>
> <tr><th align="right">Address line one:</th><td align="left">
>   <html:text property="addr1"/></td>
> <tr>
> <td align="right"><html:submit property="submit" value="Submit"/></td>
> <td align="left"><html:reset/></td>
> </tr>
> </table>
> </html:form>
> </body>
> </html>
> 
> <%--
> 
> Allow user to submit username and password to logon action.
> 
> --%>
> 
> Here's the pertinent parts of SANameAction.java:
> 
> public final class SANameAction extends Action
> {
>   public ActionForward perform(ActionMapping mapping,
>             ActionForm form,
>             HttpServletRequest request,
>             HttpServletResponse response)
>         throws IOException, ServletException
>   {
>     // Obtain fields from web tier
>     String name = ((SANameForm) form).getName();
>     String addr1 = ((SANameForm) form).getAddr1();
> 
>     // Log this event, if appropriate
>     if (servlet.getDebug() >= SAConstants.DEBUG)
>     {
>       StringBuffer message =
>         new StringBuffer("SANameAction: name = ");
>           message.append(name);
>           message.append(", addr1 = ");
>           message.append(addr1);
>           servlet.log(message.toString());
>     }
> 
>     // Forward control to the welcome URI
>     // specified in the configuration.
>     return (mapping.findForward(SAConstants.WELCOME));
>   }
> 
> } // End SANameAction
> 
> Here's the pertinent parts of SANameForm.java:
> 
> public final class SANameForm extends ActionForm
> {
>   // ---- Instance Variables
> 
>   private String name = null;
>   private String addr1 = null;
> 
>   // ---- Properties
> 
>   public String getName()
>   {
>     return (this.name);
>   }
> 
>   public void setName(String name)
>   {
>     this.name = name;
>   }
> 
>   public String getAddr1()
>   {
>     return (this.addr1);
>   }
> 
>   public void setAddr1(String addr1)
>   {
>     this.addr1 = addr1;
>   }
> 
> // ---- Public Methods
>     public void reset(ActionMapping mapping,
>         HttpServletRequest request)
>     {
>       setName(null);
>       setAddr1(null);
>     }
> 
>     public ActionErrors validate(ActionMapping mapping,
>                                  HttpServletRequest request)
>     {
>       ActionErrors errors = new ActionErrors();
>       if ((name == null) || (name.length() < 1))
>         errors.add("name",
>           new ActionError("app.error.name.required"));
>       if ((addr1 == null) || (addr1.length() < 1))
>         errors.add("addr1",
>           new ActionError("app.error.addr1.required"));
>       return errors;
>     }
> 
> } // End SANameForm
> 
> When I first execute the form via  http://localhost:8080/starrd/app/names.do
> I immediately (before entering any data or executing the submit button) I
> get validation errors displayed for name and addr1 and get the following in
> my log file:
> 
> 2002-01-21 20:19:19 action: Processing a GET for /app/names
> 2002-01-21 20:19:19 action:  Looking for ActionForm bean under attribute
> 'nameForm'
> 2002-01-21 20:19:19 action:  Creating new ActionForm instance of class
> 'com.starrcs.app.SANameForm'
> 2002-01-21 20:19:19 action:  Storing instance under attribute 'nameForm' in
> scope 'request'
> 2002-01-21 20:19:19 action:  Populating bean properties from this request
> 2002-01-21 20:19:19 action:  Validating input form properties
> 2002-01-21 20:19:19 action:   Validation error(s), redirecting to:
> /pages/app/Name.jsp
> 
> Then if I fill in the two fields with data, I do not get validation errors
> (which is correct). Then this is what is in my log:
> 
> 2002-01-21 20:19:37 action: Processing a POST for /app/names
> 2002-01-21 20:19:37 action:  Looking for ActionForm bean under attribute
> 'nameForm'
> 2002-01-21 20:19:37 action:  Creating new ActionForm instance of class
> 'com.starrcs.app.SANameForm'
> 2002-01-21 20:19:37 action:  Storing instance under attribute 'nameForm' in
> scope 'request'
> 2002-01-21 20:19:37 action:  Populating bean properties from this request
> 2002-01-21 20:19:37 action:  Validating input form properties
> 2002-01-21 20:19:37 action:   No errors detected, accepting input
> 2002-01-21 20:19:37 action:  Looking for Action instance for class
> com.starrcs.app.SANameAction
> 2002-01-21 20:19:37 action: SANameAction: name = data1, addr1 = data2
> 2002-01-21 20:19:37 action: Processing a POST for /app/welcome
> 2002-01-21 20:19:37 action:  Looking for Action instance for class
> com.starrcs.app.SAContinueAction
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>