2

I got this error:

execute="#{localeManager.changeLocale}": Property 'changeLocale' not found on type xyz.com.i18n.LocaleManager

where LocaleManager is:

@ManagedBean
@ViewScoped
public class LocaleManager implements Serializable
{
    // other codes here

    public static void changeLocale(AjaxBehaviorEvent event) {
       newLocale = (Locale) new Locale((String) event.toString());
       FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedLocale", newLocale); 
    }
}

and I'm calling the bean's method here:

<h:selectOneMenu id="selectLang" immediate="true" value="#{langListing.language}">
    <f:ajax event="change" execute="#{localeManager.changeLocale}" />
    <f:selectItems value="#{langListing.languages}" />
</h:selectOneMenu>

I'm learning AJAX by experimenting with this code. But I don't understand how Ajax evaluates bean's method. Is this a straightforward issue to resolve?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ChuongPham
  • 4,761
  • 8
  • 43
  • 53

1 Answers1

1

As per the <f:ajax> tag documentation the execute attribute should refer to a collection of client IDs which are to be processed at the server side. This should not refer to some bean action method. The exception is coming because it's expecting a getter method which returns a collection of client IDs.

You want to use the listener attribute instead.

<f:ajax listener="#{localeManager.changeLocale}" />

Note that the default event for h:selectOneMenu is already valueChange. You can just omit it.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That's great! Thanks. Is there a way to get the selected value from h:selectOneMenu? event.toString() only returns the event. I have looked through the content assist in Eclipse but couldn't find a method to return the value selected in h:selectOneMenu. – ChuongPham Feb 17 '11 at 14:37
  • Just access property behind value `#{langListing.language}`. Since it's apparently on a different managed bean, you'd like to refactor one and other e.g. change to `#{localeManager.language}` or inject one in other e.g. `@ManagedProperty(value="#{langListing}) private LangListing langListing;` inside `LocaleManager`. – BalusC Feb 17 '11 at 14:38
  • I haven't done JSF 2 injection but your example works great. I have another question but I want to figure it out for myself first before I come here to ask a question. Anyway, thanks heap and cheers. – ChuongPham Feb 17 '11 at 14:46