JSP / MySQL - 格式化没有采用

时间:2017-05-26 14:52:25

标签: java css mysql jsp

我只是试图从表tblUser中提取数据,并在下面的代码中简单地格式化isActive以显示Red&正常体重,如果显示“否”,绿色和粗体显示是。所有结果都显示为绿色和粗体。

<%@ page import="com.mysql.*" %>
<%@ page import="java.sql.*" %>

<html>
<body>
<div id="content">


    <p>Displaying table contents: </p>

    <table border="0" cellpadding="10">
        <thead>
            <tr>
                <th>User ID</th>
                <th>First Name:</th>
                <th>Last Name:</th>
                <th>Notes</th>
                <th><b>Is Active?</b></th>
            </tr>
        </thead>
        <tbody>


            <%
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = null;
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/raa", "root", "root");
                Statement stmt = null;
                stmt = conn.createStatement();
                String query = "SELECT userID, Group_id, role, fname, lname, email, password, phone, notes,  case when isActive = 1 then 'Yes' else 'no' end isActive FROM raa.tblUser";
                ResultSet rs = null;
                rs = stmt.executeQuery(query);
                while(rs.next()){
            %>
            <tr>
                <%
                    int userid = rs.getInt("userid");
                    String fname = rs.getString("fname");
                    String lname = rs.getString("lname");
                    String notes = rs.getString("notes");
                    String isActive = rs.getString("isActive");
                %>
                <td><%=userid %></td>
                <td><%=fname %></td>
                <td><%=lname %></td>
                <td><%=notes %></td>
                <%
                    String activeClass = "";
                    String activeBold = "";                
                    if(isActive == "0"){
                        activeClass = "red";
                        activeBold = "normal";
                    }
                    else{
                        activeClass = "green";
                        activeBold = "bold";
                    }
                %>
                <td style="color: <%=activeClass%>; font-weight: <%=activeBold %>;"><%=isActive%></td>
            </tr>               

            <%      
                }
            %>

        </tbody>
    </table>
</div>
</body>
</html>

这是结果页面: Results page

1 个答案:

答案 0 :(得分:1)

你必须在对象比较中使用equals ... isActive.equals(“0”)“0”.equals(isActive)

                <%
                    String activeClass = "";
                    String activeBold = "";                
                    if("0".equals(isActive)){
                        activeClass = "red";
                        activeBold = "normal";
                    }
                    else{
                        activeClass = "green";
                        activeBold = "bold";
                    }
                %>