0

Let's say I want to create a country/state picker and use it to manage the pulldown menus in a form. Ideally, I'd like to bind the values of these menus to the location Object I'm trying to manage.

<h:selectOneMenu id="countryMenu" value=#{controller.location.country}>
    <f:ajax listener="{controller.locationPicker.updateStates}"
            execute="stateMenu cityInput"/>
    <h:selectItems value=#{controller.locationPicker.countries}/>
</h:selectOneMenu>

<h:selectOneMenu id="stateMenu" value=#{controller.location.state}>
    <f:ajax listener="{controller.locationPicker.updateCities}" execute="cityInput"/>
    <h:selectItems value=#{controller.locationPicker.states}/>
</h:selectOneMenu>

<h:inputText id="cityInput" value=#{controller.location.city} 
             rendered=#{not empty controller.location.state"/>
public class LocationPicker {
.
.
    public void updateStates(AjaxBehaviorEvent event) {
        List<String> selectedCountries = getSelectionsFromEvent(event);
        states = getStatesFromCountries(selectedCountries);
        cities = getCitiesFromStates(states);
    }
}

The problem is that the only way to get the location.country or location.state values within the locationPicker, is to use the AjaxBehaviorEvent to get the value. This doesn't seem like the right approach, though, considering I have to cache all of these values within the locationPicker because there are other form elements which depend on these values and each AjaxBehaviorEvent only has the value for the component it's fire on.

It would be SO MUCH EASIER to just bind the menu values/selectItems to one object (the locationPicker). Then, when submitting the form, copy the locationPicker values to the location object. I'm trying to avoid that though!

Anyone have any recommendations on how to make this work?

wsaxton
  • 1,030
  • 15
  • 34
  • Can't you get those selected value of ``s in the associated backing bean simply by adding properties of the desired types (`String` apparently) and binding the `value` attribute of ``s to those properties? You are apparently sending those selected values to a different bean `location` (not `locationPicker`) as can be seen by `value=#{location.country}` and `value=#{location.state}` in the respective ``s. – Tiny Dec 25 '14 at 16:48
  • @Tiny Well, that's the point of my question. I don't want to add Strings to the LocationPicker just to maintain the menu values, then have to copy them to the Location object later. NOTE: I just added in a controller for clarity. Location and LocationPicker are non-JSF managed classes. Let's say that Location is a database Entity and LocationPicker is a helper class which manages country/state/city combinations. – wsaxton Dec 25 '14 at 17:59
  • @Tiny The reason I'm trying to make these as independent as possible is because the LocationPicker could be used by something else. Let's say a SearchFilter class that allowed you to choose 1 or more countries/states/cities. It isn't always just linked to a Location object. – wsaxton Dec 25 '14 at 18:06
  • Is this acceptable as dupe? http://stackoverflow.com/questions/17613029/populate-pselectonemenu-based-on-another-pselectonemenu-in-each-row-of-a-pdat Question is a bit different, but the answer seems to cover all your needs. – BalusC Dec 25 '14 at 20:15
  • @BalusC Hmm, not really. Perhaps I wasn't clear enough in my original question, but I don't want to just access the value within the controller...I want to somehow manipulate that value within the controller but still keep that value bound to my Entity. For example, if I have a state selected and then I change the country, the list of stateMenu selectItems changes making the selected state invalid. I'd want the stateMenu value to be set to "" or null. Then, because the select state value is "", the cityInput field should no longer be rendered. – wsaxton Dec 26 '14 at 04:58
  • @BalusC Just an aside: I'm doing all of this work to a) try to keep my Entity object (with its own Location object) separate from the LocationPicker's logic and b) allow me to directly attach these form field values to the Entity so that I don't have to copy them over later. Do u think this is silly or is it a reasonable design objective achievable with JSF? – wsaxton Dec 26 '14 at 05:02

0 Answers0