struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hgos...@csc.com.au
Subject Re: Re-populating form after validate fails
Date Tue, 03 Feb 2004 23:59:35 GMT
Just set up an forward for validation failure. You can do this in your 
struts-config.xml file, in fact you can have as many  forwards as you 
like, but for practical purposes most people prefer to have at most three 
or four forwards per Action. For example, you might have a SaveAction that 
forwards to the next screen in a sequence if validation succeeds, an error 
page if it fails, a login page if the session becomes invalidated, and a 
query action if you had a "delete" button to delete a record.

Just say you query a database to populate your form fields. Then you can 
edit the form and hit Save. In the Form's validate method you fill an 
ActionErrors object.

Then in the SaveAction you will have something like:

            // If a message is required, save the specified key(s)
            // into the request for use by the <struts:errors> tag.
            if (!errors.isEmpty()) 
            {
                saveErrors(request, errors);

                // Forward control to the appropriate 'failure' URI
                forward = mapping.findForward("failure");

            } 
 
            else
            {
                // Forward control to the appropriate 'success' URI if no errors
                forward = mapping.findForward("success");
                } 
 
                return forward;

and in your struts-config you will have something like:

                <action path="/saveAction"
                                type="myproject.actions.SaveAction"
                                name="somethingForm"
                                validate="true">
                    <forward name="success" path="nextForm/>
                    <forward name="failure" path="somethingForm"/>
                </action>


I hope you can figure out your answer from that... I can't give you a real 
example from my project because of confidentiality so I have to try and 
make it generic.


Cheers,
Heya Gosper
CSC Australia
212 Northbourne Ave, Braddon ACT 2612
Ph: +61 (0) 2 6246 8155  Fax: +61 (0) 2 62468100
MOB: 0401 611779
----------------------------------------------------------------------------------------
This is a PRIVATE message. If you are not the intended recipient, please 
delete without copying and kindly advise us by e-mail of the mistake in 
delivery. NOTE: Regardless of content, this e-mail shall not operate to 
bind CSC to any order or other contract unless pursuant to explicit 
written agreement or government initiative expressly permitting the use of 
e-mail for such purpose.
----------------------------------------------------------------------------------------





Paul Barry <paul.barry@nyu.edu>
04/02/2004 09:48 AM
Please respond to "Struts Users Mailing List"

 
        To:     struts-user@jakarta.apache.org
        cc: 
        Subject:        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


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs Email
Security System. For more information on a proactive email security
service working around the clock, around the globe, visit
http://www.messagelabs.com
________________________________________________________________________



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message