以下 XHTML 在请求作用域 Bean 到 <p:remoteCommand>
<p:selectOneMenu>
中设置一个选定的值。
<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。
如果,豆子由春天维护如下呢?
@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";
}
}
如何将 language
属性初始化为此 Bean 中<p:selectOneMenu>
的选定语言?
传统上,language
- 请求参数的参数值可以使用
FacesContext context = FacesContext.getCurrentInstance();
String language = context.getExternalContext().getRequestParameterMap().get("language");
像下面这样,
@Controller
@Scope("request")
public final class IntermediateLocaleBean
{
@Autowired
private final transient LocaleBean localeBean=null;
public IntermediateLocaleBean() {}
public void setLanguage(String language) {
this.language = language;
}
public String localeAction()
{
String language = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("language");
localeBean.setLocale(language.equals("hi")?new Locale(language, "IN"):new Locale(language));
return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true";
}
}
但我个人不喜欢使用 FacesContext
公开整个请求,除非绝对必要。
Spring
中有一个org.springframework.beans.factory.annotation.Value
注解,可以与 Spring 表达式语言一起使用,获取请求参数,如下所示。
@Controller
@Scope("request")
public final class IntermediateLocaleBean
{
@Value("#{request.getParameter('language')}") //Exposing the value of language.
private String language;
@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";
}
}
<p:remoteCommand>
作为请求参数提供的 language
值现在可以使用@Value
注释在此 bean 中使用(在我的情况下不需要用于language
的 getter 方法)。
如果有其他可用,请建议或添加其他答案!