Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a if statement in a div using thymeleaf?

Tags:

html

thymeleaf

If I remove the div tag from the source code below my application runs with no error. But it displays an empty cell (which is correct). I just want to hide this if the cells are empty.

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

@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();               
        }     

Snippet of Class 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;

}

This table will populate if AppPortModel.Status is CRITICAL. I am trying to hide this table if there is no values in this table. If I just have a regular div tag my code will run but I will have a awkward head and table row heads on my page with empty cells.

Attempt

I tried adding some th logic into my div tag, but I receive an null error.

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

Attempt 2

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

This script would hide my div tag. Even if I have Status = to CRITICAL it would still hide the table.

like image 431
Jodi Avatar asked Feb 06 '23 14:02

Jodi


1 Answers

You can check whether the list is empty using the following condition expression. Assuming the object showap is a List type,

<div th:if="${not #lists.isEmpty(showap)}">
     --content--
</div>

Your h3 tag and table goes inside this div.

#lists is a thymeleaf utility class. Refer http://www.thymeleaf.org/apidocs/thymeleaf/2.0.2/org/thymeleaf/expression/Lists.html for more options. You can alternatively use size() method and check for list length.

like image 72
Lucky Avatar answered Feb 08 '23 03:02

Lucky