Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with redirect after validation

 
Robert Wundisch
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can nobody help me?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use an ActionForm property instead of a parameter? Struts will automatically set ActionForm properties before returning the JSP after an error.
 
Robert Wundisch
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic