You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kevin McAbee <km...@okstate.edu> on 2004/07/21 21:47:27 UTC

App scoped bean to be used with html:optionsCollection in JSP

Hello.  I have searched through the list for this or similar issues but 
have been unable to find a good reference post to work off.  That 
disclaimer is in the hopes that I don't get thoroughly flamed if I 
somehow missed a post. :)  Also, if I am going about this the wrong 
way, *please* let me know.

I have several lists that I wish to use in forms and result pages for 
checkboxes and options.  These lists (all 50 states, various 
majors/options for college, etc.) will appear on several pages across 
the application and their values placed in the database when selected.  
I want to cache or quickly pull these lists for use via tag libraries.

My current implementation is as follows:

I have my lists stored in xml files with the following design:

<!ELEMENT list (set+)>
<!ELEMENT set (label, value) (#CDATA)>
<!ELEMENT label (#CDATA)>
<!ELEMENT value (#CDATA)>

Example:
<list>
    <set>
      <label>New York</label>
      <value>NY</value>
    </set>
</list>

I have a class called LabelValueListBean that houses an ArrayList of 
LabelValueBeans.  It has two methods of importance - populate(String 
filename) and getList().
-- getList() returns the ArrayList containing the LabelValueBeans
-- populate(String filename) is a static method that takes an xml file 
of the above format and uses Digester to create a LabelValueBean for 
each set element and to place all LabelValueBeans in an instance of 
LabelValueListBean

I then have a class that extends Plug-In to instantiate my 
application-wide objects.  All of my xml files are listed by filename 
in my ApplicationResources file.  I then use the messages as filename 
arguments for the populate() method of each LabelValueListBean..  I 
then set each LabelValueListBean as an attribute in the application 
scope.

Current code:
// msgres is a MessageResources object
// sc is the ServletContext object
String statesList = msgres.getMessage("list.states");
statesNew = LabelValueListBean.populate(sc.getRealPath(statesList));
sc.setAttribute("statesNew", statesNew);
// I now print out the LabelValueListBean
System.out.println(statesNew.toString());

The above executes without fail and the println shows that the 
LabelValueListBean is indeed populated.

I hope the above is enough background information to work with.

I have tried various methods, all of which failed, of accessing the 
LabelValueListBean object in a JSP page via tag libraries for use in 
html:optionsCollection and the like.

Attempts:
(<app:xxx> is the Jakarta application tag library)

<html:select property="homeState">
   <app:attributes id="statesTest" name="statesNew">
      <html:optionsCollection name="statesTest" property="list"/>
   </app:attributes>
</html:select>

The above returns the following:
javax.servlet.ServletException: No getter method for property list of 
bean statesTest



In my desperation:

<html:select property="homeState">
   <html:optionsCollection name='${applicationScope.statesNew}' 
property="list"/>
</html:select>

javax.servlet.ServletException: Cannot find bean Label: Alabama --- 
Value: AL
Label: Alaska --- Value: AK
Label: Arizona --- Value: AZ
... (the rest of the toString() of LabelValueListBean)



What am I doing wrong?  How can I fix this?  I am going about this a 
totally wrong way?  I figure the solution is probably quite simple and 
under my foolish nose.  Any help would be greatly appreciated.

Regards,

Kevin


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: App scoped bean to be used with html:optionsCollection in JSP

Posted by Erik Weber <er...@mindspring.com>.
I load my states in two arrays in a PlugIn -- the labels ("Alabama") and 
the values ("AL"), and set them as application scope attributes.

Then I simply refer to those attributes in my combos, like this:

<html:options name="USStateValues" labelName="USStateLabels"/>

Because it's wrapped in an html:select with a property name that matches 
one of my form bean properties, it remains populated correctly after 
form validation errors, etc.

Hope that helps, I read this kind of quickly.

Erik



Kevin McAbee wrote:

> Hello. I have searched through the list for this or similar issues but 
> have been unable to find a good reference post to work off. That 
> disclaimer is in the hopes that I don't get thoroughly flamed if I 
> somehow missed a post. :) Also, if I am going about this the wrong 
> way, *please* let me know.
>
> I have several lists that I wish to use in forms and result pages for 
> checkboxes and options. These lists (all 50 states, various 
> majors/options for college, etc.) will appear on several pages across 
> the application and their values placed in the database when selected. 
> I want to cache or quickly pull these lists for use via tag libraries.
>
> My current implementation is as follows:
>
> I have my lists stored in xml files with the following design:
>
> <!ELEMENT list (set+)>
> <!ELEMENT set (label, value) (#CDATA)>
> <!ELEMENT label (#CDATA)>
> <!ELEMENT value (#CDATA)>
>
> Example:
> <list>
> <set>
> <label>New York</label>
> <value>NY</value>
> </set>
> </list>
>
> I have a class called LabelValueListBean that houses an ArrayList of 
> LabelValueBeans. It has two methods of importance - populate(String 
> filename) and getList().
> -- getList() returns the ArrayList containing the LabelValueBeans
> -- populate(String filename) is a static method that takes an xml file 
> of the above format and uses Digester to create a LabelValueBean for 
> each set element and to place all LabelValueBeans in an instance of 
> LabelValueListBean
>
> I then have a class that extends Plug-In to instantiate my 
> application-wide objects. All of my xml files are listed by filename 
> in my ApplicationResources file. I then use the messages as filename 
> arguments for the populate() method of each LabelValueListBean.. I 
> then set each LabelValueListBean as an attribute in the application 
> scope.
>
> Current code:
> // msgres is a MessageResources object
> // sc is the ServletContext object
> String statesList = msgres.getMessage("list.states");
> statesNew = LabelValueListBean.populate(sc.getRealPath(statesList));
> sc.setAttribute("statesNew", statesNew);
> // I now print out the LabelValueListBean
> System.out.println(statesNew.toString());
>
> The above executes without fail and the println shows that the 
> LabelValueListBean is indeed populated.
>
> I hope the above is enough background information to work with.
>
> I have tried various methods, all of which failed, of accessing the 
> LabelValueListBean object in a JSP page via tag libraries for use in 
> html:optionsCollection and the like.
>
> Attempts:
> (<app:xxx> is the Jakarta application tag library)
>
> <html:select property="homeState">
> <app:attributes id="statesTest" name="statesNew">
> <html:optionsCollection name="statesTest" property="list"/>
> </app:attributes>
> </html:select>
>
> The above returns the following:
> javax.servlet.ServletException: No getter method for property list of 
> bean statesTest
>
>
>
> In my desperation:
>
> <html:select property="homeState">
> <html:optionsCollection name='${applicationScope.statesNew}' 
> property="list"/>
> </html:select>
>
> javax.servlet.ServletException: Cannot find bean Label: Alabama --- 
> Value: AL
> Label: Alaska --- Value: AK
> Label: Arizona --- Value: AZ
> ... (the rest of the toString() of LabelValueListBean)
>
>
>
> What am I doing wrong? How can I fix this? I am going about this a 
> totally wrong way? I figure the solution is probably quite simple and 
> under my foolish nose. Any help would be greatly appreciated.
>
> Regards,
>
> Kevin
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org