如何在使用Thymeleaf时在div中使用if语句?

6

如果我从下面的源代码中删除div标签,我的应用程序将没有错误。但它会显示一个空单元格(这是正确的)。如果单元格为空,我只想隐藏它。

Thymeleaf HTML

<div th:object="${AppPortModel.Status}" th:if="${AppPortModel.Status} == 'CRITICAL'">  
<h3>
MONITORING
</h3>

<table id ="apCritTable">
    <thead>
    <tr>
        <th> Status </th>
        <th> HostName </th>
        <th> Port Name</th>
        <th> Port Listening Count </th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="AppPortModel, iterStat : ${showap}" th:if="${AppPortModel.Status == 'CRITICAL'}">
        <td th:text ="${AppPortModel.Status}"></td>
         <td th:text="${AppPortModel.host}">${AppPortModel.host}</td>
        <td th:text="${AppPortModel.portOwner}"></td>
         <td th:text="${AppPortModel.count}"></td>
    </tr>
    </tbody>
</table>
</div>

AppPortModel

public class AppPortModel implements Comparable {
private String Status;
private String host;
private String portName;
private String plCount;

//getters and setters

@Override int compareTo(Object o) {
return //stuff
 }

控制器

@Controller
public class IndexController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView getdata() throws IOException {
ModelAndView model = new ModelAndView("index");
        model.addObject("showap", apList);
            return model;
}

AppPortList

@Component
    public class AppPortList {      

   @Value("#{'$APP_SERVERS_PORT}'.split('@!')}")
   private String[] apServerArray;
   @Value("#{'${APP_SERVER_MONITORING_LIST}'.split('@!')}")
   private String[] appServerPortsList;
@PostConstruct
    public List<AppPortModel> getAppPortList() {

    final int MYTHREADS = 80;
    ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);

    ApplicationPort.resetData();

    try {

        for (int z = 0; z < apServerArray.length; z++) {    

            String apServer = apServerArray[z];
            String[] portListArray=appServerPortsList[z].split(",");
            ApplicationPort apWorker = new ApplicationPort(apServer, portListArray);
            executor.execute(apWorker);
        }   
        } catch(ArrayIndexOutOfBoundsException e) {
                System.out.println("ArrayIndexOutOfBoundsException in AppPortList");
            }
            finally {
            executor.shutdown();

            while (!executor.isTerminated()) {
            }               
            logger.info("\nFinished all threads in App Port. ");    
            }
            return ApplicationPort.getData();               
        }     

App类的片段

static List<AppPortModel> apData = new ArrayList<AppPortModel>();

public static List<AppPortModel> getData() {
    return apData;
}

public static void setData(List<AppPortModel> apData) {
    ApplicationPort.apData = apData;
}

public static void resetData(){
    apData = new ArrayList<AppPortModel>();
}

public ApplicationPort(String apServer, String[] portListArray) {
this.apServer = apServer;   
this.portListArray = portListArray;

}

如果AppPortModel.Status为CRITICAL,这个表格将被填充。我想在表格中没有值的情况下隐藏它。如果我只有一个普通的div标签,我的代码会运行,但是页面上会出现尴尬的表头和空单元格。
尝试:
我尝试在我的div标签中添加一些th逻辑,但是我收到了一个null错误。
 <div th:object="${AppPortModel.Status}" th:if="${AppPortModel.Status == 'CRITICAL'}">  

尝试2

<div th:if="${Status} == 'CRITICAL'">  

这个脚本会隐藏我的 div 标签。即使我的状态等于 CRITICAL,它仍然会隐藏表格。

请问您能否发布AppPortModel的模型类?如何将对象传递到模型中?您是否检查过该对象不为空?请发布相关控制器代码。您的错误信息显示AppPortModel对象为空,并且您正在尝试从空实例访问字段。请发布完整的堆栈跟踪。 - Lucky
嗨,Lucky,你找到我的帖子了!如果我在div标签中不使用th:if,我的应用程序将运行。它将什么也不显示,因为我的<tr>只有在状态=“CRITICAL”时才会显示。我现在会立即添加所有请求的文件。 - JayC
所以如果我理解正确,您基本上想要在列表为空时隐藏表格,对吗? - Lucky
是的,隐藏 div 标签以便它也隐藏 <h3>。 - JayC
1个回答

12

你可以使用以下条件表达式来检查列表是否为空。假设showap对象是List类型,

<div th:if="${not #lists.isEmpty(showap)}">
     --content--
</div>
您的H3标签和表格应放在这个 div 中。
#lists 是一个Thymeleaf工具类。有关更多选项,请参阅http://www.thymeleaf.org/apidocs/thymeleaf/2.0.2/org/thymeleaf/expression/Lists.html。您还可以使用 size() 方法并检查列表长度。

showap列表不为空,其中包含具有不同状态的值列表,例如CRITICAL、BAD和OK。这就是为什么我想在状态值=CRITICAL时显示div。如果不是,则不显示。就像我在<tr标签>中所做的那样,绘制出所有包含“CRITICAL”的列表。 - JayC
是的,如果列表中没有任何项,它将不会显示表格。即列表大小为0。如果您的列表showap包含值,则表格将被显示。 - Lucky
嗯,那对我行不通。我的列表总是有项目的。我还有两个类似于这个坏的表格和一个好的表格。它们都共享相同的列表“showap”。看起来我必须修改我的逻辑并将它们放在单独的列表中,对吧? - JayC
是的,那会起作用。如果您有三个div元素用于三个表格,您可以基于状态创建三个不同的列表,并根据我的答案检查大小,以显示相应的表格。 - Lucky
如果我的列表是一个泛型列表 = List<AppPortModel> apData。我用一行简单的apData.add(new AppPortModel(Status, Server, pName, count))添加所有接收到的数据。我是否需要为每个创建新的POJO? - JayC
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接