簡體   English   中英

如何基於Java中的if-else條件為從結果集返回的列值添加顏色

[英]how to add color to column values returned from resultset based on if-else condition in java

我如何在java的ResultSet返回的列值的文本中添加顏色。我已經搜索並對其進行了瀏覽但沒有用。我要在文本上應用顏色的條件是

編輯1

  public LinkedHashMap<String, String> beam_CurrentStatus() throws SQLException { try { con=getConnection(); stmt = con.createStatement(); String sql="SELECT TOP 1 c.logtime, a.BL1_data_SS_ST as BeamLine1,a.BL2_data_SS_ST as BeamLine2,a.BL3_data_SS_ST as BeamLine3,a.BL4_data_SS_ST as BeamLine4,a.BL5_data_SS_ST as BeamLine5,a.BL6_data_SS_ST as BeamLine6,a.BL7_data_SS_ST as BeamLine7,a.BL8_data_SS_ST as BeamLine8,a.BL9_data_SS_ST as BeamLine9,a.BL10_data_SS_ST as BeamLine10,a.BL11_data_SS_ST as BeamLine11, a.BL12_data_SS_ST as BeamLine12,a.BL13_data_SS_ST as BeamLine13,a.BL14_data_SS_ST as BeamLine14,a.BL15_data_SS_ST as BeamLine15,a.BL16_data_SS_ST as BeamLine16,a.BL17_data_SS_ST as BeamLine17,a.BL18_data_SS_ST as BeamLine18,a.BL19_data_SS_ST as BeamLine19,a.BL20_data_SS_ST as BeamLine20,a.BL21_data_SS_ST as BeamLine21,a.BL22_data_SS_ST as BeamLine22,a.BL23_data_SS_ST as BeamLine23,a.BL24_data_SS_ST as BeamLine24,a.BL25_data_SS_ST as BeamLine25,a.BL26_data_SS_ST as BeamLine26,a.BL27_data_SS_ST as BeamLine27,b.st1_prmt_status_p45,c.beam_current,c.beam_energy from INDUS2_BLFE.dbo.main_BLFE_status a inner join INDUS2_MSIS.dbo.main_MSIS_status b on a.logtime=b.logtime inner join INDUS2_BDS.dbo.DCCT c on b.logtime=c.logtime ORDER BY c.logtime DESC "; stmt.executeQuery(sql); rs = stmt.getResultSet(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { if(rs.getInt(29)==1 && rs.getDouble(30)>110 && rs.getDouble(31)>2500) { for (int j = 2; j < 29; j++) { if (rs.getInt(j) == 1) { // al.add(rs.getInt(j)); count += 1; String name = rsmd.getColumnLabel(j); map.put(name, "Being Used"); **//Here want to show Green color text** } else { String name = rsmd.getColumnLabel(j); map.put(name, "Not Being Used"); **//Here want to show Red color text** } } } } } catch (Exception e) { System.out.println("\\nException in Bean " + e.getMessage()); } finally { closeConnection(stmt, rs, con); } return map; } 

現在我的輸出是:

BeamLines狀態

BeamLine1關閉
BeamLine2關閉
BeamLine3關閉
BeamLine4開

EDIT2

我的已編輯jsp頁面是:

 <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <style> .green { color: green; } .red { color: red; } </style> </head> <table width="500px" border = "1" cellpadding="1"> <tr> <th>BeamLines</th> <th>Status(1=Being Used)</th> </tr> <c:forEach var="country" items="${bs.beam_CurrentStatus()}"> <c:choose> <c:when test="${country.value == 'OFF'}"> <tr> <td class="green"> ${country.key} </td> <td class="green"> ${country.value} </td> </tr> </c:when> <c:otherwise> <tr> <td class="red"> ${country.key} </td> <td class="red"> ${country.value} </td> </tr> </c:otherwise> --> </c:choose> </c:forEach> </table> </body> </html> 

我想用紅色顯示關閉狀態,用綠色顯示打開狀態。 怎么做。我不使用秋千。 提前致謝。

編輯完jsp后,將不顯示任何表內容,即表為空。不要出錯。

我猜得到了...這並不難。 :)

<style>
    .green {
        color: green;
    }

    .red    {
        color: red;
}
</style>




<table width="500px" border = "1" cellpadding="1"> 

    <tr> 
        <th>BeamLines</th> 
        <th>Status(1=Being Used)</th> 
    </tr> 

    <c:forEach var="country" items="${bs.beam_CurrentStatus()}">

        <c:choose>
            <c:when test="${country.value == 'OFF'}">
            <tr> 
                <td class="green"> ${country.key} </td> 
                <td class="green"> ${country.value} </td> 
            </tr> 

            </c:when>
            <c:otherwise>
            <tr> 
                <td class="red"> ${country.key} </td> 
                <td class="red"> ${country.value} </td> 
            </tr> 
            </c:otherwise>
        </c:choose>

    </c:forEach> 

</table>

不要忘記通過在上面添加以下內容來導入C:taglib:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

如果有幫助,請標記為答案。

我看到您正在使用JSP嗎? 顏色是視圖的責任,而不是控制器的責任。 您應該在視圖的“標題”上添加適當的CSS屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM