• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

validation error causes an URL problem!!

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am coding a project based on Struts and recently I got a problem caused by validation error. the work flow is that user input data in edit.jsp and then submit to back-side EditAction.java and then should be taken to review.jsp to review their input. Please refer to following information

in struts-config.xml: (baseForm is an ActionForm)


in edit.jsp:


The url for edit.jsp is "http://serverName/webroot/edit?param1=p1¶m2=p2". When I submit the form in edit.jsp, URL will become to "http://serverName/webroot/review.do" and being sent to server side. baseForm.validate() will be executed and catch invalid input if any, then return errors to edit.jsp page agaion and show user some error message. the problem is that upon validation failure, control returns to edit.jsp with changed URL, which is "http://serverName/webroot/review.do". Therefore, when I mean to refresh edit.jsp with its original url, it actually submit the form again. Is there any ways I can change url back upon validation failure? Thanks.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that when the validator returns to the JSP, it uses the RequestDispatcher to forward to the JSP. It does not redirect to the JSP. The difference is that a forward keeps the URL of the Action class (review.do) while a redirect changes the URL to the JSP being forwarded to (edit.jsp). The down side of a redirect is that it does not pick up the contents of the request object. If you redirect to a JSP from an action class, anything the action class puts in the request is not readable by the JSP.

If you want to change this behavior, you can, but be aware that if you do, anything the action puts in the request (including error messages!) will be lost when the JSP is displayed. You will have to use the session or parameters to pass any data to the JSP.

Having given that warning, here's how you'd do it. First, you have to tell struts that whenever you specify an input for an action, you're using the name of a forward, not a path to a JSP. Example:

Then you specify a forward with a redirect="true" attribute. Example:


Note: The parameters passed to the original JSP will still not get passed using this method unless you specifically retrieve them in the action class and then re-pass them to the JSP.
 
Peixiao Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you reply, Merrill. I think the control flow even didn't reach the back-side action class. so I don't understand why you put a forward inside that action tag. In my opnion, it is the ActionServlet that invoke validate() on the form bean and stop submission if fails and then forward control to the path specified by the "input" property. So I think maybe redirect setting should be somewhere else?? just my 2 cents. thanks.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that the validation occurs before the action class is called is irrelevant. Struts would still be able to find the forward associated with the action because that is the action associated with the form submission.
[ March 01, 2007: Message edited by: Merrill Higginson ]
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Therefore, when I mean to refresh edit.jsp with its original url, it actually submit the form again. Is there any ways I can change url back upon validation failure?



What you are talking about is standard behavior with Struts. Are you saying that the issue is when the user clicks the Refresh button in the browser? The user could always just hit the back button instead. The Back and Refresh buttons do tend to cause issue with dynamic web applications, but I don't really see this case as much of a problem.

The key to the code change put forth my Merrill is the controller inputForward settings. This tells Struts that all input tags contain the name of a forward to use when validation fails.

If you really want to avoid this behavior maybe you could switch to a AJAX style of validation where validation would take place before the form was submitted. I know this can be done, but I don't know if there is a solution that would integrate with Struts.

- Brent
 
Peixiao Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. One more question: where do I specify the forward name, which is gonna be used when validation fails somewhere? In action class?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to this question is in my previous example:

In this example, by specifying input="backToInput" I'm telling Struts that if there is any validation error to forward to the ActionForward named "backToInput".

Be aware that since you changed the controller to inputForward="true" whenever you specify the input attribute in an action mapping, it must refer to a forward, and not the URI of a JSP. This is true for all action mappings, not just this one.
 
Peixiao Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that works...thank you guys so much.
 
Peixiao Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merrill, as you mentioned above, I lost all erros message because of redirecting. Here is my validate() in ActionForm bean



in edit.jsp page,


I thought maybe I can save errors in session but not sure how to display ActionErrors stored in session? any suggestions? thanks again.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe it should work if you specify scope="session" in your action path definition.
reply
    Bookmark Topic Watch Topic
  • New Topic