如何为 JSP 中的字符串结果着色?

How to Colorize String Results in JSP?

在我的 jsp 文件中最后 <%=rs.getString("results")%> as results returns 3 个不同的结果作为 W、L 和 D。 如何使用 if 条件 and/or 其他语句显示 W 代表绿色,L 代表红色,D 代表黄色?

如果有任何问题,我将代码添加到此 link:https://codeshare.io/EBbOyO

在此先感谢那些愿意提供帮助的人。

    <table class="table ">
  <thead>
    <tr class="d-flex">
      <th class="text-center col-xs-1" style="width:1%" scope="col">Week</th>
      <th class="text-center col-xs-1"  scope="col">Match Date</th>
      <th  class="text-left  col-xs-1" scope="col">Home</th>
      <th  class="text-left  col-xs-1" scope="col">Away</th>
      <th class="text-center col-xs-1" style="width:5%"  scope="col">Home Score</th>
      <th class="text-center col-xs-1" style="width:5%"  scope="col">Away Score</th>
            <th class="text-center col-xs-1" style="width:5%"  scope="col">Result</th>
    </tr>

<%
    if(request.getParameter("uid")!=null)
    {
        int id=Integer.parseInt(request.getParameter("uid")); 
        String dburl="jdbc:mysql://localhost:3306/teams"; 
        String dbusername="root";
        String dbpassword="Fener2013"; 

        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver"); 
            Connection con=DriverManager.getConnection(dburl,dbusername,dbpassword); 
            PreparedStatement pstmt=null; //create statement

            pstmt=con.prepareStatement("select m.week, m.match_date, t1.team_name hometeam, m.home_score as homescore, t2.team_name awayteam,m.away_score as awayscore,l.league_name,  CASE WHEN m.home_score > m.away_score THEN 'W' WHEN m.home_score < m.away_score THEN 'L' WHEN m.home_score = m.away_score THEN 'D' END AS results from matches m join teams  t1 on m.home_team_id = t1.team_id join teams  t2 on m.away_team_id = t2.team_id join   leagues l on l.league_id = m.league_id where  m.home_team_id = ? or m.away_team_id = ?");          
            pstmt.setInt(1,id);
            pstmt.setInt(2,id); 
            ResultSet rs=pstmt.executeQuery(); 

            while(rs.next())
            {
                %> 
  <tbody>
    <tr class="d-flex">
      <td class="something col-xs-1" style="width:1%"><%=rs.getString("m.week")%></td>
      <td class="something col-xs-1" ><%=rs.getString("m.match_date")%></td>
      <td class="text-left col-xs-1" ><%=rs.getString("hometeam")%></td>
      <td class="text-left col-xs-1" ><%=rs.getString("awayteam")%></td>
      <td class="something col-xs-1" style="width:5%"  ><%=rs.getString("homescore")%></td>
      <td class="something col-xs-1" style="width:5%"  ><%=rs.getString("awayscore")%></td>
<!--     Below Strings returns 3 different result as W,L and D. How can I show W for green, L for red and D for yellow for using if or other statement? -->
         
    <td class="something col-xs-1" style="width:5%"> <span class="label label-success"><%=rs.getString("results")%></span></td>
    
    </tr>
         </tbody>
                <%
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
%>
</table> 
<span class='label label-success color-<%=rs.getString("results")%>'><%=rs.getString("results")%></span>

您可以将结果传递给 class,并添加样式

<style>
.color-W{
color:green;
}
.color-L{
color:red;
}
.color-D{
color:yellow;
}
</style>