aspose file tools*
The moose likes Struts and the fly likes validation error causes an URL problem!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validation error causes an URL problem!!" Watch "validation error causes an URL problem!!" New topic
Author

validation error causes an URL problem!!

Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
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.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
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

Joined: Feb 15, 2005
Posts: 4864
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 ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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

Joined: Oct 04, 2004
Posts: 28
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Oct 04, 2004
Posts: 28
that works...thank you guys so much.
Peixiao Lin
Greenhorn

Joined: Oct 04, 2004
Posts: 28
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

Joined: Feb 15, 2005
Posts: 4864
I believe it should work if you specify scope="session" in your action path definition.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validation error causes an URL problem!!
 
Similar Threads
Display Check Boxes & Dropdowns in Struts with search results
Problem in Client side validation using Struts
Struts validation not displaying error messages
Struts 1.1 using RAD7
Problem in Client Side Validation using Struts