2013-06-11 92 views
2

我需要计算在jsf数据表中收到的金额并以这些字段的总和显示。JSF表达式语言操作和总和值字段

A)我需要总和所有计算在EL值表达式中的值,并显示在total1字段中。 B)我需要将在EL表达式2中计算出的所有项目总和并显示在total2字段中。

C)我需要total1 + total2和总计字段中显示。

Obs .:我收到来自DatabaseBean的值。

<p:dataTable id="table" value="#{valueBean.values}" var="item"> 
<p:column > 
<h:outputText id="value1" value="#{(item[0]*160)/100}" /> 
<f:facet name="footer"> 
    <h:outputText id="total1" value="#...????" /> 
</f:facet> 
</p:column > 
<p:column > 
<h:outputText id="value2" value="#{(item[1]*160)/100}" /> 
<f:facet name="footer"> 
    <h:outputText id="total2" value="#...????" /> 
</f:facet> 
</p:column > 
<h:outputText id="total" value="#...????" /> 
... 
</p:dataTable> 

回答

1
  1. 旅游首先指出。做价值=“#{(item [0] * 160)/ 100}”这样的事情是不好的编程习惯。由于各种原因,但显然对于可维护性,未来的开发人员会发现它必须理解为什么160或100更不用说如果他们改变如何安全地更改这些值
  2. 取决于值中的项是否是有效的“数值”值有一个可以很容易地处理服务器端,而不是UI层

最安全的策略是采用豆使用计算和保存你需要显示的UI

- 你的所有值上的错误听起来像一个绿色的程序员,所以我会给你一个腿。但是,如果您仔细思考,下面的代码仍然可以使用一些改进。

-I已经将Item bean定义为下面的内部类,但是如果您对此感到满意,您总是可以将它分解为自己的类。

- 此策略允许您在需要的地方添加支票,并在需要的地方添加错误处理,但我没有收入。

- 我还假定初始数据来自DatabaseBean的列表,该列表在数组中保存了一些2个值。

import java.util.ArrayList; 
import java.util.List; 

public class BagOfItems { 

    private static int COMMON_MULTIPLIER = 160;//here document where this comes from 
    private static int COMMON_DIVISOR = 100; //here document where this comes from 

    private class Item{ 
     private int value1; 
     private int value2; 
     private int calculatedValue1; 
     private int calculatedValue2; 

     public Item(int[] values){ 
      value1 = values[0]; 
      value2 = values[1]; 
      calculateValues(); 
     } 

     public int getValue1() { 
      return value1; 
     } 
     public void setValue1(int value1) { 
      this.value1 = value1; 
     } 
     public int getValue2() { 
      return value2; 
     } 
     public void setValue2(int value2) { 
      this.value2 = value2; 
     } 

     public int getCalculatedValue1() { 
      return calculatedValue1; 
     } 

     public void setCalculatedValue1(int calculatedValue1) { 
      this.calculatedValue1 = calculatedValue1; 
     } 

     public int getCalculatedValue2() { 
      return calculatedValue2; 
     } 

     public void setCalculatedValue2(int calculatedValue2) { 
      this.calculatedValue2 = calculatedValue2; 
     } 

     public void calculateValues(){ 
      calculatedValue1 = (value1 * COMMON_MULTIPLIER)/COMMON_DIVISOR; 
      calculatedValue2 = (value2 * COMMON_MULTIPLIER)/COMMON_DIVISOR; 
     } 
    } 


    private int totalValues1 = 0; 
    private int totalValues2 = 0; 

    private List<Item> items = new ArrayList<Item>(); 

    public BagOfItems(List<DatabaseBean> databaseBeans){ 
     initItems(databaseBeans); 
    } 

    private void initItems(List<DatabaseBean> databaseBeans){ 
     for(DatabaseBean databaseBean : databaseBeans){ 
      Item item = new Item(databaseBean.values); 
      items.add(item); 
     } 
    } 

    protected void sum(){ 
     for(Item item : items){ 
      totalValues1 += item.getCalculatedValue1(); 
      totalValues2 += item.getCalculatedValue2(); 
     } 
    } 

    public int getTotalValues1() { 
     return totalValues1; 
    } 

    public void setTotalValues1(int totalValues1) { 
     this.totalValues1 = totalValues1; 
    } 

    public int getTotalValues2() { 
     return totalValues2; 
    } 

    public void setTotalValues2(int totalValues2) { 
     this.totalValues2 = totalValues2; 
    } 

    public List<Item> getItems() { 
     return items; 
    } 

    public void setItems(List<Item> items) { 
     this.items = items; 
    } 

    public int getTotal(){ 
     return (totalValues1 + totalValues2); 
    } 

} 

的UI然后会是这个样子

<p:dataTable id="table" value="#{bagOfItems.items}" var="item"> 
<p:column > 
<h:outputText id="value1" value="#{item.calculatedValue1}" /> 
<f:facet name="footer"> 
    <h:outputText id="total1" value="#{bagOfItems.totalValues1}" /> 
</f:facet> 
</p:column > 
<p:column > 
<h:outputText id="value2" value="#{item.calculatedValue2}" /> 
<f:facet name="footer"> 
    <h:outputText id="total2" value="#{bagOfItems.totalValues2}" /> 
</f:facet> 
</p:column > 
<h:outputText id="total" value="#{bagOfItems.total}" /> 
... 
</p:dataTable>