my dog learned polymorphism
The moose likes Struts and the fly likes Where to create Collections for html:options Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Where to create Collections for html:options" Watch "Where to create Collections for html:options" New topic

Where to create Collections for html:options

Mark Kennedy

Joined: Jan 16, 2003
Posts: 3
Hello, all:
I'm building a struts app and have a question regarding the proper place to generate the Collection objects that I need to use with the html ptions tag.
I have a utility class that has static methods that return various Collections that I need to generate options for select elements. Each method requires one argument. The argument is retrieved from the User bean which is kept in session scope.
The following code illustrates what I have working now:

The thing is, I'd like to get that scriptlet block out of there. I've tried generating "optionsList" in the Action class and storing it in request scope, but then the JSP fails if the Form bean's validate() method returns errors since the Action's execute() method never gets called. So, I then moved the "optionsList" code to the Form bean, but that fails if the user isn't logged in (since the "optionsList" can only be built with the argument that comes from the User bean, which only exists if the user is logged in). The check to see if the user is logged in happens in the Action so I can forward them to the login form if need be.
Sorry this is long-winded.
So, my question is: with the code above, can I replace the scriptlet block with some combination of logic or bean tags? I want to put the code that generates the "optionsList" in the JSP, but I don't want it to be in a scriptlet, if at all possible.
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
An Action class should be placing things such as Collections and any other objects you might need into the proper scope.
Let's say your JSP is called "myForm.jsp". Somewhere this page needs to be called in order to be displayed to the user. Since JSPs in struts should never be called directly, there should be an Action which calls this. Let's call this Action "ViewMyFormAction". Additionally let's say that your form submits to "ProcessMyFormAction". Here's some code:
Excerpt of struts-config.xml

Excerpt of

Excerpt of myForm.jsp

Excerpt from

Because your jsp was not called directly and was forwarded to by another Action, that Action can place the necessary objects in scope for reference by the JSP.
To handle errors you have a couple of choices. One thing you can do is if you want to keep your objects in "request" scope, in the Action called by the form, set validate="false" and call the forms validate() method in the Action, saving any errors and returning to "input" after placing any necessary objects in scope. This wouldn't be my first choice because it's kind of redundant to recreate the objects.
A better thing you could do to handle your problem with errors is place the objects in a scope other than "request", such as "session", when the ViewMyFormAction is called. If you go this route, you should remove the objects from session in This is how I did it in the example code. Now if your options list is something that's fairly static and the same for every user, then it might be appropriate to put it in application scope.
Mark Kennedy

Joined: Jan 16, 2003
Posts: 3
Thanks for the detailed reply. I appreciate the help.
I eventually came up with the solution to store the beans that I needed in the user's session scope. That way, I know that they will be available to the JSP in either case.
Thanks again! It really helps!
I agree. Here's the link:
subject: Where to create Collections for html:options
It's not a secret anymore!