remoteCommand向Spring

remoteCommand向Spring

本文介绍了通过p:remoteCommand向Spring bean提供参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following XHTML sets a selected value in <p:selectOneMenu> to a request scoped bean through <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:

The corresponding JSF managed 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,因此可以完成所有操作.

The language property is initialized to the selected language in <p:selectOneMenu>. This is all done as it is a JSF managed bean.

如果,该bean由Spring维护,如下所示?

What if, the bean is maintained by Spring like as follows?

@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中的<p:selectOneMenu>中将language属性初始化为所选的语言?

How to initialize the language property to the selected language in <p:selectOneMenu> in this bean?

推荐答案

传统上,可以使用

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公开整个请求,除非绝对必要.

But I personally dislike exposing the entire request using FacesContext unless it is absolutely necessary.

有一个 org.springframework.beans.factory.annotation.Value 批注可与Spring表达式语言一起使用,以获取请求参数,如下所示.

There is an org.springframework.beans.factory.annotation.Value annotation in Spring that can be used with the Spring expression language to obtain request parameters as follows.

@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方法).

The value of language supplied by <p:remoteCommand> as a request parameter is now available in this bean using the @Value annotation (the getter method for language is not required in my case).

这篇关于通过p:remoteCommand向Spring bean提供参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:57