*
The moose likes Struts and the fly likes Problem with redirect after validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with redirect after validation" Watch "Problem with redirect after validation" New topic
Author

Problem with redirect after validation

Robert Wundisch
Greenhorn

Joined: Jun 05, 2008
Posts: 4
hello,
i am new in this forum and my english is not so good. i am sorry.

i have a little problem with the validation (not validation-framework) in a ActionForm. the ActionForm includes the method validate(..) and returns the errors.

when i use wrong characters in the form and the validation starts i get an error. the ActionForm can not redirect to the jsp because the ActionForm doesn't know the parameter in the url.

example: my url looks like: edit.do?thing=8 but the validation-method will redirect to edit.do and this url is not allowed to used without an parameter.

my question: can i give the validate the parameter or how can i solve this problem?

thanks for your answers
Robert Wundisch
Greenhorn

Joined: Jun 05, 2008
Posts: 4
can nobody help me?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Why not use an ActionForm property instead of a parameter? Struts will automatically set ActionForm properties before returning the JSP after an error.


Merrill
Consultant, Sima Solutions
Robert Wundisch
Greenhorn

Joined: Jun 05, 2008
Posts: 4
hmm.. i don't know what you mean because i'm a newbie at Struts.

can you explain more precisely or give me an code-example?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
just code the following in your ActionForm bean:




In the Jsp, you can access the field using:


I think you would benefit by going through a Struts tutorial to get a better handle on Struts basics before proceeding further with your project. Here is a link to several of them:

http://wiki.apache.org/struts/StrutsTutorials
[ June 07, 2008: Message edited by: Merrill Higginson ]
Robert Wundisch
Greenhorn

Joined: Jun 05, 2008
Posts: 4
hi merrill,

here is my code:

the ActionForm


and my JSP productEdit in a Tiles-Skeleton


When use productEdit.do?prod=8 and the validation-method in the ActionForm find an error in the form then it redirect to productEdit.do
But i can't call the jsp without any prod-parameter.

Can i use your solution for my problem? I can't imagine how to use the property in this case.
[ June 07, 2008: Message edited by: Robert Wundisch ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This application would be fine for a regular Servlet/JSP project, but it is not a good Struts project. The main problem is that there is too much logic in the JSP. The Struts way is to do as much as possible in the Action and as little as possible in the JSP. This wouldn't work at all with Struts validation because if the page were redisplayed because of an error, it would revert back to the original database values, and not display the values entered by the user.

I'd suggest you refactor as follows:
  • Create a new PrepareProductEdit Action that forwards to this JSP and call the action in your link or URL instead of the JSP
  • Change the parameter that is passed from prod to prodID and make it so the ProductEditForm bean has the same properties as the Product class
  • Add logic to the new PrepareProductEdit Action to get the data from the database and populate the ActionForm bean's properties
  • Remove all Database retrieval logic from the JSP
  • Remove all value attributes from the Struts html:text tags. Struts will automatically populate the value of these fields with the value of the ActionForm properties

  • Here's an example of what I'm talking about:

    PrepareProductEditAction class:


    the JSP:


    [ June 07, 2008: Message edited by: Merrill Higginson ]
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Problem with redirect after validation
     
    Similar Threads
    Validate of Form data
    Struts Design Problem
    Dispatch VS LookupDispatch
    where to put validation?
    How do I redirect to different method of DispatchAction when validation fails?