稍后编辑:问题我应该如何以及何时从数据库加载h:dataTable 的模型解决了我的问题!
使用dataTable时,请小心正确初始化项目列表!
在包含拍卖的表格中,我尝试更新拍卖的状态 - 使用selectOneMenu
元素进行选择。
问题是所选值未更新 - 我在提交表单后继续在关联行上获得拍卖的旧值。
如何将所选值作为changeState()的参数发送?
auctions.xhtml
<h:dataTable value="#{auctionService.getAuctions()}" var="auction" class="table table-striped">
<h:column>
<f:facet name="header">Activ/inactiv</f:facet>
<h:selectOneMenu value="#{auction.active}" class="form-control" >
<f:selectItems value="#{auction.valuesActive}" />
</h:selectOneMenu>
<h:column>
<f:facet name="header">Modifica stare</f:facet>
<h:commandButton action="#{auctionService.changeState(auction, auction.active)}" value="Modifica stare" class="btn btn-primary"></h:commandButton>
</h:column>
</h:column>
</h:dataTable>
</h:form>
Auction Bean中的重要代码:
@ManagedBean(name="auction", eager=true)
@RequestScoped
@Entity
public class Auction implements Serializable{
/**
*
*/
private int active; //plus getters and setters which I did not copy here
@Transient
private List<SelectItem> valoriActiv = new ArrayList<SelectItem>(Arrays.asList(new SelectItem(0), new SelectItem(1))); //plus getters and setters which I did not copy here
auctionService bean包含这个重要的方法:
public String changeState(Auction l, int activ){
auctionsDAO.changeState(l, activ);
return "auctions.xhtml";
}
auctionDAO服务类中的相应代码(只是一个负责数据库操作的类 - 我使用JPA进行数据库访问):
public void changeState(Auction licitatie, int activ){
System.out.println("in metoda");
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Auction auctionHelper = em.find(Auction.class, licitatie.getAuctionID());
auctionHelper.setActiv(activ);
tx.commit();
em.close();
}
我还发布了一张我希望实现的图片,以防万一我的解释不够: