I'm trying to use p:ajax tag inside a h:selectOneMenu but the actionListener (AccountManager.countrySelected) is not beeing invoked.
The xhtml page fragment:
Code: Select all
<h:form prependId="false" >
<h:selectOneMenu id="country" required="true" value="#{AccountManager.selectedCountry}" >
<f:selectItems value="#{AccountManager.availableCountries}" />
<p:ajax event="change" partialSubmit="true" update="city" actionListener="#{AccountManager.countrySelected}"/>
</h:selectOneMenu>
</h:form>
Code: Select all
public class AccountManager implements Serializable {
private static final long serialVersionUID = 927489238947992834L;
private AccountService accountService;
private CityRepository cityRepository;
private CountryRepository countryRepository;
// The account being managed.
private Account account;
// Available genders.
private SelectItem[] availableGenders;
// Available countries.
private List<SelectItem> availableCountries;
// Available cities.
private List<SelectItem> availableCities;
private Country selectedCountry;
public AccountManager() {
}// end constructor
@PostConstruct
public void init() {
// Create an empty Account.
this.account = new Account();
Person person = new Person();
person.setHomeAddress(new HomeAddress());
this.account.setPerson(person);
// Populate the availableGenders list.
List<SelectItem> list = new ArrayList();
for(Gender g : Gender.values()) {
SelectItem item = new SelectItem(g, g.toString());
list.add(item);
}
availableGenders = list.toArray(new SelectItem[0]);
// Populate the availableCountries list.
availableCountries = new ArrayList();
List<Country> countryList = countryRepository.listAll();
for(Country c : countryList) {
availableCountries.add(new SelectItem(c, c.getName()));
}
populateAvailableCities((Country) availableCountries.get(0).getValue());
}// end method
/**
* Invoked when a country is selected in the GUI.
* @param actionEvent
*/
public void countrySelected(ActionEvent actionEvent) {
System.out.println("#### countrySelected.");
populateAvailableCities(selectedCountry);
}// end method
public void validateLogin(FacesContext context, UIComponent component, Object input) {
String var = (String) input;
if ("averri".equalsIgnoreCase(var)) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"<strong>" + var + "</strong>" + " already exists!", null));
}
}// end method
public void validateEmail(FacesContext context, UIComponent component, Object input) {
String var = (String) input;
if ("alexandreverri@gmail.com".equals(var)) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"<strong>" + var + "</strong>" + " already exists for another account!", null));
}
}// end method
/**
*
* @param country
*/
public void populateAvailableCities(Country country) {
availableCities = new ArrayList();
if(country == null) {
return;
}
List<City> cities = cityRepository.findByCountryCode(country.getCode());
for(City c : cities) {
availableCities.add(new SelectItem(c, c.getName()));
}
}// end method
public String requestAccount() {
try {
accountService.requestAccount(account);
addInfoMessage("Sucess", "Account created");
} catch (AccountServiceException e) {
addWarnMessage("Error", e.getMessage());
}
return null;
}// end method
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public void setCityRepository(CityRepository cityRepository) {
this.cityRepository = cityRepository;
}
public void setCountryRepository(CountryRepository countryRepository) {
this.countryRepository = countryRepository;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
/**
* @return the availableGenders
*/
public SelectItem[] getAvailableGenders() {
return availableGenders;
}
/**
* @param availableGenders the availableGenders to set
*/
public void setAvailableGenders(SelectItem[] availableGenders) {
this.availableGenders = availableGenders;
}
/**
* @return the availableCountries
*/
public List<SelectItem> getAvailableCountries() {
return availableCountries;
}
/**
* @param availableCountries the availableCountries to set
*/
public void setAvailableCountries(List<SelectItem> availableCountries) {
this.availableCountries = availableCountries;
}
/**
* @return the selectedCountry
*/
public Country getSelectedCountry() {
return selectedCountry;
}
/**
* @param selectedCountry the selectedCountry to set
*/
public void setSelectedCountry(Country selectedCountry) {
this.selectedCountry = selectedCountry;
}
/**
* @return the availableCities
*/
public List<SelectItem> getAvailableCities() {
return availableCities;
}
/**
* @param availableCities the availableCities to set
*/
public void setAvailableCities(List<SelectItem> availableCities) {
this.availableCities = availableCities;
}
}// end class