Struts - How to set the previously selected value when using Collection / LabelValueBean

Hi,

I'm using a collection and LabelValueBean to render a list of years in JSP (select/options drop down).  Then the user selects a year and submits, next time when they goto the page I want to make sure previously selected year is shown in the select drop down... normally I use form.setXXXX method and works, but here i'm using a labelValuebean and form.setter method is not working.. is there any other way around when you use collection/LabelValueBean?


*** Here is the Java code which generates a range of years in my Action Servlet

          private static Collection years = new ArrayList();
          static {
              Calendar c = Calendar.getInstance();
              int year = c.get(Calendar.YEAR);
              int endyear = year - 10;
              years.add(new LabelValueBean("Select", ""));
              for (int i = (year-80); i <= endyear; i++) {
                  years.add(new LabelValueBean(String.valueOf(i), String.valueOf(i)));
              }
          }

*** Then I pass this into my requrest object

         request.setAttribute("years", years);

*** rendered in JSP using this code:
                                                <html:select property="year">
                     <html:options collection="years" property="value" labelProperty="value" />
                                    </html:select>

       This renders the option list,
Ilan_MoorthyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bloodredsunCommented:
If you are using the request scope to store the actionForm then I don't think so, as there is no "selectedId" property for that tag. If you stored the actionform in the session then it should select the populated option automatically.

Have you tried using <html:optionsCollection>? I've never used it but it appears to be specifically designed for creating options from a collection (and with a name like that who would have guessed!).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raj3060Commented:
Save the formbean in session in your action class after you process request. Usually it's done automatically, bt some times it don't work.
And at the top of your JSP render the form from session.

<%
FormName formName = (FormName) session.getAttribute("formName");
%>

This should automatically select any previously selected option.
Ilan_MoorthyAuthor Commented:
optionCollections works... I need to pass the collection via the form.... create a private Collection Var in form and in Action class assign the collection.. it worked...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.