使用字符串形式的货币列表,如下所示。
<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()
,最终根据当前值进行重定向viewId
,includeViewParams=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
已添加仅用于装饰。这是行不通的。
由于listener
in无法根据of<p:ajax>
完成的导航案例结果进行重定向,因此无论如何都必须使用。action
<p|h:commandXxx>
ExternalContext#redirect()
<p:remoteCommand>
可以在完成时使用,<p:ajax>
但这将不必要地涉及到服务器的两次往返,首先将货币值设置为关联的托管 bean,然后进行重定向。
如何includeViewParams=true
在给出的示例中进行重定向?