以下 XHTML 通过 将选定的值设置<p:selectOneMenu>
为请求范围的 bean <p:remoteCommand>
。
<h:form id="languageForm" prependId="true">
<pe:blockUI target=":body" widgetVar="blockBodyUIWidget">
<h:panelGrid columns="2">
<h:graphicImage library="default" name="images/ajax-loader1.gif" class="block-ui-image"/>
<h:outputText value="#{messages['blockui.panel.message']}" class="block-ui-text"/>
</h:panelGrid>
</pe:blockUI>
<p:selectOneMenu id="languages" value="#{localeBean.language}" onchange="changeLanguage([{name:'language', value:this.value}]);">
<f:selectItem itemValue="en" itemLabel="#{messages['languages.english']}" />
<f:selectItem itemValue="hi" itemLabel="#{messages['languages.hindi']}" />
</p:selectOneMenu>
<p:remoteCommand name="changeLanguage" process="@this" update="@none" onstart="PF('blockBodyUIWidget').block();" oncomplete="PF('blockBodyUIWidget').unblock();" action="#{intermediateLocaleBean.localeAction}"/>
</h:form>
对应的 JSF 托管 bean:
@ManagedBean
@RequestScoped
public final class IntermediateLocaleBean
{
@ManagedProperty("#{param.language}")
private String language;
@ManagedProperty("#{localeBean}")
private LocaleBean localeBean; //Injecting another session scoped bean here.
public IntermediateLocaleBean() {}
public void setLanguage(String language) {
this.language = language;
}
public void setLocaleBean(LocaleBean localeBean) {
this.localeBean = localeBean;
}
public String localeAction()
{
localeBean.setLocale(language.equals("hi")?new Locale(language, "IN"):new Locale(language));
return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true";
}
}
该language
属性被初始化为 中的选定语言<p:selectOneMenu>
。这一切都完成了,因为它是一个 JSF 托管 bean。
如果,bean 由 Spring 维护,如下所示?
@Controller
@Scope("request")
public final class IntermediateLocaleBean
{
//Do something to initialize the property - language.
//@ManagedProperty would not work as this bean is managed by Spring.
//It is not initialized to the selected language in <p:selectOneMenu>.
//It is null.
private String language;
//The session scoped bean is injected using the @Autowired annotation as follows.
@Autowired
private final transient LocaleBean localeBean=null;
public IntermediateLocaleBean() {}
public void setLanguage(String language) {
this.language = language;
}
public String localeAction()
{
localeBean.setLocale(language.equals("hi")?new Locale(language, "IN"):new Locale(language));
return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true";
}
}
如何在此 bean中将language
属性初始化为所选语言?<p:selectOneMenu>