This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validation and data flow

 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
I have a jsp page that accepts some bean and populate the page as the following:
<jsp:include page="/page.jsp" flush="true">
<jsp aram name="data" value="data"/>
</jsp:include>
so if there's no bean "data" present, this jsp will not display anything.
Typically an action forwards to this page after doing an request.setAttribute("data",data); to create the "data" bean.

Now the form on this jsp submits to an action, if there's validation error, it returns to the current jsp. However when it returns, the bean is no longer present..
why? shouldn't the data still exist in the request?
how do I resolve this issue?
thanks!!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The trouble here is that when a bean is scoped to request, the request and all it's attributes are gone after the page is displayed. When Struts sends it back to the page after a validation error, you are have a different request context from the one you had when you generated the page.

There are a couple of ways around this:
  • put the bean in session scope - or even application scope. (If this data populates a select box that is the same for everybody, there's no need for each user to have a copy in their session)
  • Put code in the bean's reset() method to get the data and put it in the request. Since reset() is called before validate(), this will ensure that the data is there if validate() forwards back to the jsp.

  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic