如果数组列表在 JSP 页面中为空,如何删除类

how to remove class if array list is empty inside jsp page?

本文关键字:何删除 删除 列表 数组 JSP 如果      更新时间:2023-09-26

我有以下代码,如果数组为空recordList我想删除tbl-content

<table id="gradient-style">
 <tbody class="tbl-content">
  <tr>
         <%
            for (RecordBean record : recordList) {
           // some code here to get result      
         }              
        %>
        <%
            if (recordList.isEmpty()) 
          {
        %>            
            <tr>
               <td colspan="12" align="center" style="color: red;font-family: verdana"> 
                  <h3>No Search records </h3>
              </td>
           </tr>
            <%      
               }
            %>
       </tbody>
   </table>

这是 CSS

 .tbl-content{   
     height: 650px;;
     overflow: auto;
     position: absolute;
     border: 1px solid gray;
     border-top: none;  
     }

试试这个服务器端内联代码

<tbody class="<%= recordList.isEmpty()?"":"tbl-content" %>">

您可以直接在脚本标记中编写 JSTL 代码。

 <script>    
<c:if test="${empty recordList}">
//write code here to remove class
</c:if>
</script>

使用 Java 表达式语言。使用 Java Scriptlet 不是好习惯。另外,您要注意不要将JSTL与JavaScript一起使用,这是一个混合的问题。

<tbody class=" ${empty recordList ? '' : 'tbl-content' }" >
这样做

<table id="gradient-style">
 <tbody
<%
            if (!recordList.isEmpty()) 
          {
        %> 
class="tbl-content"
 <%      
               }
            %>
>
  <tr>
         <%
            for (RecordBean record : recordList) {
           // some code here to get result      
         }              
        %>
        <%
            if (recordList.isEmpty()) 
          {
        %>            
            <tr>
               <td colspan="12" align="center" style="color: red;font-family: verdana"> 
                  <h3>No Search records </h3>
              </td>
           </tr>
            <%      
               }
            %>
       </tbody>
   </table>