Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

html:multibox... + validation == No collection found

 
Mikael Vedek
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i've been struggling with this problem whole day and thought that someone of you gurus could help me.

The problem is <html:multibox and validating results which leads to javax.servlet.ServletException: No collection found.

In the action I populate the actionForm with LabelValueBeans



And in the ActionForm I have



and in the view (.jsp)



Everything works just perfect IF the other fields of the form are valid but if there's something caught by ActionErrors validate()-method I get:

javax.servlet.ServletException: No collection found

If I understand correctly this is due to the fact that the collection is not anymore in the request, right? If I create a constructor for the ActionForm with some static LabelValueBeans the validation works just fine.

Any ideas how to proceed?

P.S: Yes, I know accessing DB directly from the action is a bad thing. I will learn to use Hibernate as soon as I have some spare time

- Mikael
 
Frank VanOor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "No collection found" message comes from the JSP. It means that the collection holding all available checkboxes is empty. But, how did it become empty ? It becomes empty by the validate method.
So, when the validate method returns ActionErrors, the JSP is re-displayed. But this time, the collection of available checkboxes is empty or null and you'll see the "No collection found" error message.

solution: When the validate method creates ActionError objects, make sure you populate the collection of available checkboxes before the validate method exits.


 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank's solution will work, but if you're going to re-create the possible options, I prefer to do it by overriding the reset() method in the Actionform and placing the logic there. This always gets called before validate() and it seems cleaner to put the rebuild logic there and save the validate() method only for validation logic.

There is also another solution: If you think about it, the possible options are probably the same, no matter who is calling the page, right? If this is the case, why not just build this array of options once and store it in application scope? In my opinion, the best solution would be to modify the logic in your action to check for the existence of the array like this:

(LabelValueBean[])getServlet().getServletContext().getAttribute("possibleOptions");

If this value is null, create it from the database and place it in the servletContext.

Then modify your <logic:iterate> tag so that name="possibleOptions" and there is no properties attribute. Struts will then find the object in application scope.

This is more efficient as it avoids going to the database over and over again to retrieve the same data. This will not work, however, if the possible options are not the same for every user.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic