You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Paul Barry <pa...@nyu.edu> on 2004/02/03 23:48:50 UTC

Re-populating form after validate fails

I feel bad asking this question because I know it has been asked 1 million times in this group, but I still haven't 
found the answer so I am asking it again.  How do you get a form to re-populate if validation fails?  Here is what I am 
trying.

I have 2 actions:

<action path="/myaccount/login" type="myapp.struts.actions.ForwardAction" scope="request"
             name="LoginForm" validate="false" parameter="/WEB-INF/jsp/myaccount/login.jsp" />
<action path="/myaccount/loginAction" type="myapp.struts.actions.LoginAction" scope="request"
             name="LoginForm" validate="true" input="/myaccount/login.do" />

So, the user goes to /myaccount/login.do, and they are presented with a form.  The code for that looks like this:

<html:html>
   <body>
    <html:form method="POST" action="/loginAction.do">
       Username:&nbsp;<html:text property="username"/><br />
       Password:&nbsp;<html:password property="password" /><br />
       <html:submit/>
     </html:form>
   </body>
</html:html>

The code for the LoginForm is simple, getters and setters with a validate method like this:

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		Object form;
		if(StringUtil.isEmpty(getUsername())) {
			if(log.isDebugEnabled()) {
				log.debug("No username was entered.");
			}
			errors.add("message",
				new ActionError("myaccount.login.error.missingUsername"));
			errors.add("username",
				new ActionError("myaccount.login.error.label"));
			return errors;
		}
		
		if(StringUtil.isEmpty(getPassword())) {
			if(log.isDebugEnabled()) {
				log.debug("No password was entered.");
			}
			errors.add("message",
				new ActionError("myaccount.login.error.missingPassword"));
			errors.add("password",
				new ActionError("myaccount.login.error.label"));
			return errors;
		}
		return errors;
	}


What I am struggling with is that when I submit this form, if username is not null, but password is, the username field 
is not populated with the value for username.  I made my own ForwardAction by copying the source code for the struts 1.1 
ForwardAction and adding a few debugging statements.  When I enter a username but no password oand submit the form, I 
get back to the form, but the username field is not filled in.  Here is log:

org.apache.struts.action.RequestProcessor - Processing a 'POST' for path '/myaccount/loginAction'
org.apache.struts.action.RequestProcessor -  Storing ActionForm bean instance in scope 'request' under attribute key 
'LoginForm'
org.apache.struts.action.RequestProcessor -  Populating bean properties from this request
org.apache.struts.action.RequestProcessor -  Validating input form properties
myapp.struts.forms.LoginForm - No password was entered.
org.apache.struts.action.RequestProcessor -  Validation failed, returning to '/myaccount/login.do'
org.apache.struts.action.RequestProcessor -  Delegating via forward to '/myaccount/login.do'
org.apache.struts.action.RequestProcessor - Processing a 'POST' for path '/myaccount/login'
org.apache.struts.action.RequestProcessor -  Storing ActionForm bean instance in scope 'request' under attribute key 
'LoginForm'
org.apache.struts.action.RequestProcessor -  Populating bean properties from this request
org.apache.struts.action.RequestProcessor -  Looking for Action instance for class myapp.struts.actions.ForwardAction
org.apache.struts.action.RequestProcessor -   Returning existing Action instance
myapp.struts.actions.ForwardAction - Path=[/WEB-INF/jsp/myaccount/login.jsp],Form=[Username=dfgfdgfd,Password=]
org.apache.struts.action.RequestProcessor - 
processForwardConfig(ForwardConfig[name=null,path=/WEB-INF/jsp/myaccount/login.jsp,redirect=false,contextRelative=true])


What I don't get is that when I print out the toString of my LoginForm in the debug statement of my ForwardAction, the 
username property is populated.  So why doesn't the html:form populate the html:test field with that value?

And for extra credit, is there any way to not use the path to another action in the input property of an ActionMapping? 
    In the example above, I would rather user a forward titled say "login", than hard-code "/myaccount/login.do" for the 
input of /myaccount/loginAction.







---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Re-populating form after validate fails

Posted by Rick Reumann <st...@reumann.net>.
Paul Barry wrote:

> <action path="/myaccount/login" 
> type="myapp.struts.actions.ForwardAction" scope="request"
>             name="LoginForm" validate="false" 
> parameter="/WEB-INF/jsp/myaccount/login.jsp" />
> <action path="/myaccount/loginAction" 
> type="myapp.struts.actions.LoginAction" scope="request"
>             name="LoginForm" validate="true" input="/myaccount/login.do" />
> 
> So, the user goes to /myaccount/login.do, and they are presented with a 
> form.  The code for that looks like this:
> 
> <html:html>
>   <body>
>    <html:form method="POST" action="/loginAction.do">
>       Username:&nbsp;<html:text property="username"/><br />
>       Password:&nbsp;<html:password property="password" /><br />
>       <html:submit/>
>     </html:form>
>   </body>
> </html:html>

<snip>

> What I am struggling with is that when I submit this form, if username 
> is not null, but password is, the username field is not populated with 
> the value for username.   

My guess is the problem lies in that it looks like when validation fails 
you are forarding to another action. I could be wrong though. Usually 
the input value is a jsp page or tile and not an Action (at least as far 
as I'm aware). You're probably getting a whole new Request object built 
when you go to the login.do action and are losing your request scoped 
form. Try changing the login.do to the name of the jsp page of your 
login form.


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org