我正在尝试显示格式化的数字,其中该数字的分数位于与数字本身不同的字段中。一个简单的例子是:1234是普通数字,2是fractionDigit,它合并为12.34
像这样使用<f:convertNumber/>
:
<f:convertNumber minFractionDigits="#{car.priceDecimals}" maxFractionDigits="#{car.priceDecimals}"/>
无处不在,但在数据表列中。如果使用primefaces数据表或基本JSF数据表,则无关紧要。这是Facelet:
<h:form id="form" prependId="true">
<h1>Primefaces Datatable</h1>
<p:dataTable id="cars1" var="car" value="#{carBean.carsSmall}" >
<p:column headerText="Manufacturer">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Price Normal">
<h:outputText value="#{car.price}" />
</p:column>
<p:column headerText="Decimals">
<h:outputText value="#{car.priceDecimals}" />
</p:column>
<p:column headerText="Price converted priceDecimals">
<h:outputText value="#{car.price}">
<f:convertNumber minFractionDigits="#{car.priceDecimals}" maxFractionDigits="#{car.priceDecimals}"/>
</h:outputText>
</p:column>
<p:column headerText="Price converted hardcoded">
<h:outputText value="#{car.price}">
<f:convertNumber minFractionDigits="2" maxFractionDigits="2"/>
</h:outputText>
</p:column>
</p:dataTable>
</h:form>
这里是模型类(省略了getter和setter):
public class Car implements Serializable {
/** The model. */
private String model;
/** The year. */
private int year;
/** The manufacturer. */
private String manufacturer;
/** The color. */
private String color;
/** The price. */
private BigDecimal price;
/** Number of decimals. */
private Integer priceDecimals;
public Car(final String model, final int year, final String manufacturer, final String color,
final BigDecimal price, final int priceDecimals) {
this.model = model;
this.year = year;
this.manufacturer = manufacturer;
this.color = color;
this.price = price;
this.priceDecimals = priceDecimals; }
}
这是我的豆子
@Named
@ViewScoped
public class CarBean implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The Constant colors. */
private final static String[] colors;
/** The Constant manufacturers. */
private final static String[] manufacturers;
static {
colors = new String[10];
colors[0] = "Black";
colors[1] = "White";
colors[2] = "Green";
colors[3] = "Red";
colors[4] = "Blue";
colors[5] = "Orange";
colors[6] = "Silver";
colors[7] = "Yellow";
colors[8] = "Brown";
colors[9] = "Maroon";
manufacturers = new String[10];
manufacturers[0] = "Mercedes";
manufacturers[1] = "BMW";
manufacturers[2] = "Volvo";
manufacturers[3] = "Audi";
manufacturers[4] = "Renault";
manufacturers[5] = "Opel";
manufacturers[6] = "Volkswagen";
manufacturers[7] = "Chrysler";
manufacturers[8] = "Ferrari";
manufacturers[9] = "Ford";
}
/** The cars small. */
private List<Car> carsSmall;
public CarBean() {
this.carsSmall = new ArrayList<Car>();
this.populateRandomCars(this.carsSmall, 9);
}
private void populateRandomCars(final List<Car> list, final int size) {
for (int i = 0; i < size; i++) {
list.add(new Car(this.getRandomModel(), this.getRandomYear(), this.getRandomManufacturer(), this
.getRandomColor(), this.getRandomPrice(), this.getRandomDecimals()));
}
}
public List<Car> getCarsSmall() {
return this.carsSmall;
}
private int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}
private String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}
private String getRandomManufacturer() {
return manufacturers[(int) (Math.random() * 10)];
}
private String getRandomModel() {
return UUID.randomUUID().toString().substring(0, 8);
}
private BigDecimal getRandomPrice() {
return new BigDecimal("12.34");
}
private Integer getRandomDecimals() {
return new Integer(2);
}
public String[] getManufacturers() {
return manufacturers;
}
public String[] getColors() {
return colors;
}
}
因此它以“硬编码”方式工作,但EL表达式按指定评估为java.lang.Integer。我在MyFaces 2.0.5(Websphere)和JBoss7上使用Mojarra 2.1.3进行了测试
EL表达式适用于c:forEach,但由于一些奇怪的原因,它在数据表中不起作用。
有什么想法吗?