2012-10-02 26 views
1

在標籤如直接看到內部的功能,如果兩次會更容易理解我的問題,這個問題變的styleClass屬性裏面:調用JSF標籤

<h:outputText value="#{prod.actualStock}" 
styleClass=" 
#{productBean.getSeverity(prod.actualStock, prod.replacementAlertLevel).equals('INFO') ? 
'severity-info' : productBean.getSeverity(prod.actualStock, prod.replacementAlertLevel).equals('WARN') ? 
'severity-warn' : 'severity-danger'}" /> 

現在,請注意,我調用兩個乘以'getSeverity()'函數,三次返回中的每一次都爲outputText提供了不同的樣式類。只有保持相同的邏輯才能調用函數嗎?

''標籤放在表格中。

回答

0

爲什麼不只是讓getSeverity方法返回類名作爲字符串?

+0

當然,對不起。需要清除我的思想!哈哈。謝謝! –

+1

您應該將業務邏輯與表示邏輯分開。從託管bean設置CSS值不是一個好的解決方案。它應該有一個屬性來處理嚴重性值並在JSF代碼中進行比較。 –

1

您可以在ProductBean類持有ProductBean#getSeverity結果添加另一個屬性並設置它在你的託管bean在<h:dataTable>

@ViewScoped 
@ManagedBean 
public class Bean { 
    private List<ProductBean> productBean; 
    //getters and setters... 

    //I'm assuming you fill the list here 
    @PostConstruct 
    public void init() { 
     productBean = ... 
     for(ProductBean pb : productBean) { 
      pb.setSeverityValue(pb.getSeverity(<parameters>)); 
     } 
    } 
} 

在您的JSF代碼中使用它之前,你只需要調用屬性

<h:outputText value="#{prod.actualStock}" 
    styleClass="#{productBean.severityValue.equals('INFO') ? 'severity-info' : productBean.severityValue.equals('WARN') ? 'severity-warn' : 'severity-danger'}" /> 
+0

+1!正如你在我現在刪除的答案中所評論的,你是對的。我想他是在調用一個bean方法,但是當我看到你的評論時,我仔細檢查了這個問題並注意到了這一點。考慮到這一點,我提供的答案(和你的替代方案)是在接收產品作爲參數並返回CSS類的bean中創建一個方法。 – Gamb