1 year ago

#362696

test-img

JohnNewman

Thymeleaf will not show values in table but shows correct number of rows

I have built an application in Spring and Thymeleaf and I have two input one for phone and one for email. The one for phone works very well and the one for email (when I search for an email) returns only the row names but without values, but with the correct number of rows. I console.logged the list in Java and the list that I put in the model for email contains values.

Here is the Controller:

@PostMapping("/")
public String postDbRequest(@RequestParam(name="text2",required = false)String telefon, Model model, @RequestParam(name="email", required = false)String email) {

    if(email == null) {
        List<Map<String,Object>> mapList = new ArrayList<>();
        List<Map<String,Object>> mapList2 = new ArrayList<>();

        mapList = service.getRaspuns(telefon.substring(1, telefon.length()));
        mapList2 = service.getRaspuns2(telefon);

        model.addAttribute("map_list", mapList);
        model.addAttribute("map_list2", mapList2);
        model.addAttribute("telefon",telefon);


    }

    if(telefon == null ) {

        List<Map<String,Object>> mapList3 = new ArrayList<>();
        List<Map<String,Object>> mapList4 = new ArrayList<>();

        mapList3 = service.getRaspuns3(email);
        mapList4 = service.getRaspuns4(email);

        model.addAttribute("email_list", mapList3);
        model.addAttribute("email_list2", mapList4);
        model.addAttribute("email", email);
    }

    return "index";
}

Here is the Thymeleaf page from which you search and on which you display results:

<!DOCTYPE html>
<html xmlns:th ="http://www.thymeleaf.org" >
<head>
    <meta charset ="UTF-8" ></meta>
    <meta name ="viewport" content ="width=device-width, initial-scale=1, shrink-to-fit=no" >
    <!-- Read CSS -->
    <link rel ="stylesheet" th:href ="@{/webjars/bootstrap/css/bootstrap.min.css}" >
    <link rel ="stylesheet" th:href ="@{/stil.css}" >
    <!-- Read JS -->
    <script th:src ="@{/webjars/jquery/jquery.min.js}" defer ></script>
    <script th:src ="@{/webjars/bootstrap/js/bootstrap.min.js}" defer ></script>
    <title> Cautare telefon sau e-mail</title>
</head>
<body class ="bg-light" >
    <h2 text-align="center">Cautare telefon sau email</h2>
    <div class ="text-center" >
        <form method ="post" action ="/" >
            <div class="form-group">
                <input th:type="text" minlength="10" maxlength="10" class="form-control" name ="text2"  placeholder="Numar telefon" pattern="[0-9]{10}" />

                <input type ="submit" value ="Cauta" class ="btn btn-primary" margin-top="2%"/>
            </div>
        </form>
        <br></br>
        <form method ="post" action ="/" >
            <div class ="form-group" >
                <input type ="email" class ="form-control" placeholder ="Adresa email" 
                name ="email" />

                <input type ="submit" value ="Cauta" class ="btn btn-primary" margin-top="2%"/>
            </div>
        </form>
        <p th:text="${telefon == null} ? '' : 'Ati cautat numarul de telefon: ' + ${telefon}"></p>
        <p th:text="${email == null} ? '' : 'Ati cautat adresa de email: ' + ${email}"></p>

        <table class="table">
            <tbody>
                <tr th:each="map : ${map_list}" >

                    <td th:text="${map_list == null} ? '' : 'CNP:'"></td>
                    <td th:text ="${map.get('cif') == null} ? '' : ${map.get('cif')}" ></td>
                    <td th:text="${map_list == null} ? '' : 'Nume:'"></td>
                    <td th:text ="${map.get('den_client') == null} ? '' : ${map.get('den_client')}" ></td>
                </tr>
            </tbody>
        </table>
        
        <table class="table">
            <tbody>
                <tr th:each="map : ${map_list2}" >

                    <td th:text="${map_list2 == null} ? '' : 'CNP:'"></td>
                    <td th:text ="${map.get('cnp') == null} ? '' : ${map.get('cnp')}" ></td>
                    <td th:text="${map_list2 == null} ? '' : 'Nume:'"></td>
                    <td th:text ="${map.get('nume') == null} ? '' : ${map.get('nume')}" ></td>
                    <td th:text="${map_list2 == null} ? '' : 'Prenume:'"></td>
                    <td th:text ="${map.get('prenume') == null} ? '' : ${map.get('prenume')}" ></td>
                </tr>
            </tbody>
        </table>

        <table class="table">
            <tbody>
                <tr th:each="map2 : ${email_list}" >

                    <td th:text="${email_list == null} ? '' : 'CNP:'"></td>
                    <td th:text ="${map2.get('cif') == null} ? '' : ${map2.get('cif')}" ></td>
                    <td th:text="${email_list == null} ? '' : 'Nume:'"></td>
                    <td th:text ="${map2.get('den_client') == null} ? '' : ${map2.get('den_client')}" ></td>
                </tr>
            </tbody>
        </table>

        <table class="table">
            <tbody>
                <tr th:each="map2 : ${email_list2}" >

                    <td th:text="${email_list2 == null} ? '' : 'CNP:'"></td>
                    <td th:text ="${map2.get('cnp') == null} ? '' : ${map2.get('cnp')}" ></td>
                    <td th:text="${email_list2 == null} ? '' : 'Nume:'"></td>
                    <td th:text ="${map2.get('nume') == null} ? '' : ${map2.get('nume')}" ></td>
                    <td th:text="${email_list2 == null} ? '' : 'Prenume:'"></td>
                    <td th:text ="${map2.get('prenume') == null} ? '' : ${map2.get('prenume')}" ></td>
                </tr>
            </tbody>
        </table>
    </div>

</body>
</html>

Can someone explain to me what I am doing wrong and why the email values would not show up in the table even tough the can be console logged so they exist ? Maybe someone can give me a code example. Thanks

java

spring

spring-mvc

thymeleaf

spring-thymeleaf

0 Answers

Your Answer

Accepted video resources