在使用JSF中,最常用的恐怕就要属于表格的处理了。使用DataModel可以方便地进行对表行的处理:
比如,在Goods类中有一个goodsList存放了当前的所有商品,页面的代码为:
<f:view>
<h:form>
...
<h:dataTable value="#{Goods.goodsList}" var="goods">
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
<h:commandLint action="#{Goods.select}" immediate="true">
<h:outputText value="#{goods.name}"/>
</h:commandLink>
</h:column>
...
</h:form>
</f:view>
Goods类如下:
public class Goods{
....
private DateModel goodsList;
...
public DataModel getGoodsList(){
if(goodsList == null){
goodsList = new DataModel();
}
goodsList.setWrappedData(getRealGoodsList()); //这里通过Service层或者Dao层访问到数据库
retrun goodsList;
}
public String select(){
Goods selectedGoods = (Goods)goodsList.getRowData();
setSelectedGoods(selectedGoods);
return "success";
}
....
}