如果我从源代码中删除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标签中添加一些逻辑,但是我收到一个空错误。
<div th:object="${AppPortModel.Status}" th:if="${AppPortModel.Status == 'CRITICAL'}">
尝试2
<div th:if="${Status} == 'CRITICAL'">
此脚本会隐藏我的div标签。即使我有Status = to CRITICAL,它仍会隐藏表格。
答案 0 :(得分:4)
您可以使用以下条件表达式检查列表是否为空。假设对象showap是List类型,
<div th:if="${not #lists.isEmpty(showap)}">
--content--
</div>
你的h3标签和表进入了这个div。
#lists
是百万美元实用类。有关更多选项,请参阅http://www.thymeleaf.org/apidocs/thymeleaf/2.0.2/org/thymeleaf/expression/Lists.html。您也可以使用size()方法并检查列表长度。