使用JSF </h:selectonemenu>中的<h:selectonemenu>处理货币

时间:2014-03-26 18:30:53

标签: jsf currency jsf-2.2

我在模板<h:selectOneMenu>的标题上使用了<p:layoutUnit position="north".../>,如下所示。

<h:selectOneMenu value="#{currencyRateBean.currency}" onchange="submit();">
    <f:selectItems var="row" value="#{currencyBean.currency}" itemLabel="#{row}" itemValue="#{row}"/>
</h:selectOneMenu>

此列表使用代表货币列表的List<String>填充。该列表存储在应用程序范围的bean CurrencyBean中。

涉及的JSF托管bean如下。

@ManagedBean
@SessionScoped
public final class CurrencyRateBean implements Serializable
{
    private static final long serialVersionUID = 1L;
    private String currency;
    private BigDecimal currencyRate;

    public CurrencyRateBean() {}

    @PostConstruct
    private void init()
    {
        currencyRate=new BigDecimal(1);
    }

    public BigDecimal getCurrencyRate() {
        return currencyRate;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) throws MalformedURLException, IOException
    {
        BufferedReader in = null;
        URLConnection connection;

        try
        {
            URL url = new URL("http://www.exchangerate-api.com/INR/"+currency+"/1?k=FQRxs-xT2tk-NExQj");
            connection = url.openConnection();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String jsonObject = "";
            String line;

            while ((line = in.readLine()) != null)
            {
                jsonObject += line;
            }

            this.currencyRate = new Gson().fromJson(jsonObject, BigDecimal.class);
            this.currency = currency;
        }
        finally
        {
            if(in!=null){in.close();}
        }
    }
}

在更改setCurrency()<h:selectOneMenu>中的货币时调用onchange="submit();"方法,这是一个POST请求

完成此请求后,刷新页面时会出现重复的页面提交。为避免此重复提交,在发出此POST请求后,应出现GET Http请求。

如何做到这一点?

如果有更好的,新的,精确的替代方案来处理多币种申请,请建议。

1 个答案:

答案 0 :(得分:0)

尝试此操作,并尽可能避免使用onchange

<h:selectOneMenu value="#{currencyRateBean.currency}">
<p:ajax event="valueChange" update="here write the id(s) of component(s) or "@all" to rerender the whole page (which is really bad)" process="@this" partialSubmit="true"/>
<f:selectItems var="row" value="#{currencyBean.currency}" itemLabel="#{row}" itemValue="#{row}"/>
</h:selectOneMenu>