【问题标题】:Current element in a h:dataTableh:dataTable 中的当前元素
【发布时间】:2014-01-02 13:42:10
【问题描述】:

所以我有以下代码:

<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() 方法应该对修复进行投标,只是我不知道如何将表中的当前修复提供给 placeBid() 方法。我希望我可以用#{r} 来做,但这不是编译。我想以某种方式将每个修复存储在列表中,以便我可以在我的方法中为表中的每一行访问它。

【问题讨论】:

标签: java spring jsf datatable


【解决方案1】:

您尝试过这种语法吗?

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

根据this answer,如果使用正确的 EL 版本 (2.2),这应该可以工作,请参阅 here 另一个示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 2013-03-01
    • 2012-12-01
    • 2013-03-23
    相关资源
    最近更新 更多