h:dataTable中的当前元素

时间:2014-01-02 13:42:11

标签: java spring jsf datatable

所以我有以下代码:

<h:dataTable value="#{findRepairsBean.repairs}" var="r"
    styleClass="repair-table"
    headerClass="repair-table-header"
    rowClasses="repair-table-odd-row,repair-table-even-row">                       
    <h:column>
        <f:facet name="header">Product</f:facet>
        #{r.item.productName}
    </h:column>
    <h:column>
        <f:facet name="header">Brand</f:facet>
        #{r.item.brand}
    </h:column>
    <h:column>
        <f:facet name="header">Category</f:facet>
        #{r.item.category}
    </h:column>
    <h:column>
    <!-- column header -->
    <f:facet name="header">Defect</f:facet>
    <!-- row record -->
    #{r.details.defect}
    </h:column>

    <h:column>
    <f:facet name="header">Description</f:facet>
    #{r.details.description}
    </h:column> 
    <h:column>
    <f:facet name="header">Status</f:facet>
    #{r.details.status}
    </h:column>
    <h:column>
         <h:outputLabel for="price" value="Price: "/>
         <h:inputText id="price" value="#{findRepairsBean.price}"/>
         <h:message for="price" styleClass="error" /> 
    </h:column>
    <h:column>
         <h:commandButton styleClass="mybutton" 
            value="Place Bid" action="#{findRepairsBean.placeBid(???)}"/>
    </h:column>
</h:dataTable>

这是我的豆子:

@Component
@Scope("session")
public class FindRepairsBean implements Serializable{
    @Autowired
    private RepairService repairService;

    @Autowired
    private UserService userService;

    private String keyWord;
    private int price;
    private List<Repair> repairs;
    //private Repair repair;

    @Inject
    private UserBean userBean;

    public void findRepairs(){
        repairs = repairService.findRepairsByKeyword(keyWord);
    }

    public void placeBid(Repair repair){
        try {
            Repairer repairer = (Repairer) userService.getUser(userBean.getUsername());
            repairService.placeBid(repairer, repair, price);
        } catch(UserServiceException ex){
            Logger.getLogger(TestRepairCafe.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(ex.getLocalizedMessage())); // Add global message
        }
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public List<Repair> getRepairs() {
        return repairs;
    }

    public void setRepairs(List<Repair> repairs) {
        this.repairs = repairs;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

这是修复模型:

public class Repair implements Comparable<Repair>, Serializable
{
    private static int repairCounter = 0;
    private final int repairId;
    private Client client;    
    private Repairer repairer;    
    private RepairDetails details;
    private Item item;        
    private final List<Bid> bids;        

    private Repair()
    {
        this.repairId = repairCounter++;
        this.bids = new ArrayList<>();                
    }

    public Repair(Item item, RepairDetails repDetails)
    {
        this();
        this.item = item;
        this.details = repDetails;
    }

    public void addBid(Bid bid)
    {
        this.bids.add(bid);
    }

    public void addClient(Client client)
    {
        this.client = client;
    }

    public void setRepairer(Repairer repairer)
    {
        this.repairer = repairer;
    }

    public int getRepairId()
    {
        return repairId;
    }

    public List<Bid> getBids()
    {
        return bids;
    }

    public RepairDetails getDetails()
    {
        return details;
    }

    public Client getClient()
    {
        return client;
    }

    public Repairer getRepairer()
    {
        return repairer;
    }

    public Item getItem()
    {
        return item;
    }

    public void setDetails(RepairDetails details)
    {
        this.details = details;
    }

    public void setItem(Item item)
    {
        this.item = item;
    }

    @Override
    public int compareTo(Repair o)
    {
        return this.details.getSubmitDate().compareTo(o.getDetails().getSubmitDate());
    }

    @Override
    public String toString()
    {
        return String.format("%s -%s", this.getDetails().getDescription(), item.getProductName());
    }

}

现在,在这个xhtml页面中,我正在制作包含关键字的所有修复的数据表,这是完美的。但是我的bean中的placeBid()方法应该对Repair进行出价,只是我不知道如何将表中的当前Repair给placeBid()方法。我希望我可以用#{r}来做,但这不是编译。我想以某种方式将每个修复存储在列表中,以便我可以在我的方法中为表中的每一行访问它。

1 个答案:

答案 0 :(得分:0)

您是否尝试过这种语法?

<h:commandButton styleClass="mybutton"  
   value="Place Bid" action="#{findRepairsBean.placeBid(r)}"/>

根据this answer,如果使用正确的EL版本(2.2),这应该有效,请参阅here另一个例子。