2

使用字符串形式的货币列表,如下所示。

<p:selectOneMenu id="currency"
                 value="#{currencyRateBean.currency}"
                 onchange="changeCurrency([{name: 'currency', value: this.value}]);">

    <f:selectItems var="row"
                   value="#{currencyBean.currencies}"
                   itemLabel="#{row}"
                   itemValue="#{row}"/>
</p:selectOneMenu>

沿着一个<p:remoteCommand>.

<p:remoteCommand ignoreAutoUpdate="true"
                 name="changeCurrency"
                 partialSubmit="true"
                 process="@this"
                 update="@none"
                 action="#{currency.currencyAction}"/>

<p:remoteCommand>设置货币值的托管 bean作为参数通过上述方法传递给 JavaScript 函数。

@Named
@RequestScoped
public class Currency {

    @Inject
    @HttpParam
    private String currency;

    @Inject
    private CurrencyRateBean currencyRateBean;

    public Currency() {}

    public String currencyAction() throws MalformedURLException, IOException {

        try (Scanner scanner = new Scanner(new URL("http://www.exchangerate-api.com/INR/" + currency + "/1?k=FQRxs-xT2tk-NExQj").openConnection().getInputStream(), "UTF-8");) {
            currencyRateBean.setCurrencyRate(scanner.nextBigDecimal());
            currencyRateBean.setCurrency(currency);
        } catch (UnknownHostException | ConnectException e) {}

        return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true&includeViewParams=true";
    }
}

然后将提供的货币值CurrencyRateBean从上面的操作方法中设置为另一个会话范围的托管 bean currencyAction(),最终根据当前值进行重定向viewIdincludeViewParams=true这很重要。


现在,故事发生了变化,什么时候#{currencyRateBean.currencies}已更改为具有复合对象列表,到目前为止,该列表一直是字符串列表。

以下场景将不适用,includeViewParams=true这很重要。

<p:selectOneMenu value="#{currencyRateBean.currencyHolder}">

    <f:selectItems var="row" value="#{currencyBean.currencies}"
                   itemLabel="#{row.currency}"
                   itemValue="#{row}"/>

    <p:ajax event="change"
            listener="#{currency.currencyAction}"
            partialSubmit="true"
            process="@this"
            update="@none"/>
</p:selectOneMenu>
public void currencyAction()  throws IOException {
    // ...
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String viewId = facesContext.getViewRoot().getViewId();

    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.redirect(externalContext.getRequestContextPath() + viewId + "?includeViewParams=true");
}

includeViewParams=true已添加仅用于装饰。这是行不通的。

由于listenerin无法根据of<p:ajax>完成的导航案例结果进行重定向,因此无论如何都必须使用。action<p|h:commandXxx>ExternalContext#redirect()

<p:remoteCommand>可以在完成时使用,<p:ajax>但这将不必要地涉及到服务器的两次往返,首先将货币值设置为关联的托管 bean,然后进行重定向。

如何includeViewParams=true在给出的示例中进行重定向?

4

1 回答 1

3

faces-redirect=trueincludeViewParams=true仅适用于导航结果,不适用于您传递给的“普通” URL ExternalContext#redirect()

使用NavigationHandler#handleNavigation().

FacesContext context = FacesContext.getCurrentInstance();
String outcome = viewId + "?includeViewParams=true";
context.getApplication().getNavigationHandler().handleNavigation(context, null, outcome);

或者,使用OmniFaces

Faces.navigate(viewId + "?includeViewParams=true");

一个可疑的替代方法是自己收集所有视图参数并将它们转换为查询字符串,以便您可以使用ExternalContext#redirect()。使用 OmniFaces 更容易。

Faces.redirect(viewId + "?" + Servlets.toQueryString(Faces.getViewParameterMap()));
于 2016-03-08T20:07:37.377 回答