通过p:remoteCommand将参数提供给Spring bean

时间:2014-03-31 14:59:49

标签: spring jsf primefaces jsf-2.2 remotecommand

以下XHTML通过<p:selectOneMenu><p:remoteCommand>中的选定值设置为请求范围的bean。

<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>属性初始化为所选语言?

1 个答案:

答案 0 :(得分:2)

传统上,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方法不需要我的情况)。

如果有其他可用的内容,请建议或添加其他答案!