所以,我试图在这个问题中实现 Anthony/BalusC 的方法:如何在 JSF 数据表中获取选定的行索引?但无济于事。我还阅读了@BalusC 的“使用数据表”文章(一如既往地精彩),但那是为 JSF1.2 编写的,而2.0 文章并没有真正解决获取选定行数据的问题。当用户单击“添加到我的收藏夹”按钮时,选定的行不会传递给支持 bean,导航案例不会被遵循,并且当前页面被刷新。
有什么想法我在这里做错了吗?
这是我的支持bean:
@ManagedBean(name = "selectedBean")
@RequestScoped
public class SelectedBeerBean
{
private List<Beer> favoriteBeers;
private Beer selectedBeer;
private HtmlDataTable datatableBeers;
public HtmlDataTable getDatatableBeers() {
return datatableBeers;
}
public void setDatatableBeers(HtmlDataTable datatableBeers) {
this.datatableBeers = datatableBeers;
}
public String addBeer()
{
selectedBeer = (Beer) datatableBeers.getRowData();
return "selectedBeer";
}
public List<Beer> getFavoriteBeers() {
return favoriteBeers;
}
public void setFavoriteBeers(List<Beer> favoriteBeers) {
this.favoriteBeers = favoriteBeers;
}
public Beer getSelectedBeer() {
return selectedBeer;
}
public void setSelectedBeer(Beer selectedBeer) {
this.selectedBeer = selectedBeer;
}
}
这是我的 xhtml 页面:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Welcome to Draft Savvy, #{draftSavvyController.name}</title>
</h:head>
<h:form>
<h:body style="background-image: url(Background-Wood.png); ">
<h3>You searched for "#{draftSavvyController.searchTerm}"</h3>
<h4>Here are your beers</h4>
<h:dataTable binding="#{selectedBean.datatableBeers}" value="#{draftSavvyController.beerList}" var="beer" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Logo"/>
</f:facet>
<h:graphicImage url="#{beer.icon}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Beer Name"/>
</f:facet>
<h:outputText value="#{beer.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Description"/>
</f:facet>
<h:outputText value="#{beer.description}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Beer ID"/>
</f:facet>
<h:outputLabel value="#{beer.id}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Add To My Favorites"/>
</f:facet>
<h:commandButton value="Add Beer" action="#{selectedBean.addBeer}">
<f:setPropertyActionListener target="#{selectedBean.selectedBeer}" value="#{beer}" />
</h:commandButton>
</h:column>
</h:dataTable>
</h:body>
</h:form>
</html>