Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSpring-Thymeleaf将空对象返回控制器_Java_Spring_Model View Controller_Thymeleaf - Fatal编程技术网

JavaSpring-Thymeleaf将空对象返回控制器

JavaSpring-Thymeleaf将空对象返回控制器,java,spring,model-view-controller,thymeleaf,Java,Spring,Model View Controller,Thymeleaf,我有一个页面,其中包含有关数据库中计算器结果的信息。当我尝试单击按钮进入下一页时,如果我想查看更多详细信息,请单击java catch exception: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null! 为什么我会有这个错误 我的控制器: @GetMapping("/form") public

我有一个页面,其中包含有关数据库中计算器结果的信息。当我尝试单击按钮进入下一页时,如果我想查看更多详细信息,请单击java catch exception:

The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!
为什么我会有这个错误

我的控制器:

@GetMapping("/form")  
    public String selectData(Model model){  
        model.addAttribute("calcResults", calculatorRepository.findAll());
        return "user/form";  
    } 
-

-


结果
历史
身份证件
数据Wyjazdu
数据Przyjazdu
阿克贾
普泽利茨

您的绑定不正确。您缺少两件事:

  • 使用th:field属性而不是th:text属性。这就是id没有绑定到相应对象的原因
  • 看起来对象绑定到“calcResults”是一个对象列表。我从来没有这样做过。我认为动态绑定一般不起作用。解决方法:创建一个具有数组属性的包装类CalcResult,例如调用items(替换列表)…+以下代码:

    <tr th:each="calc, stat : *{items}">
      <input type="text"
         th:field="*{items[__${stat.index}__].id}">
      /* other fields of the objects stored in the array has to be insert here like in the line above */
    </tr>
    
    
    /*存储在数组中的对象的其他字段必须像上面的行一样插入此处*/
    
  • <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <html xmlns="http://www.w3.org/1999/xhtml" 
          xmlns:th="http://www.thymeleaf.org">
        <head>
            <link rel="stylesheet" href="style.css" type="text/css"/>
            <script
      src="https://code.jquery.com/jquery-3.3.1.min.js"
      ></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/printThis/1.12.3/printThis.js"></script>
    
            <title>Result</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <link type="text/css" rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
        </head>
        <body>
                <form action="#" th:action="@{/show}" th:object="${calcResults}" method="post">
                <div class="col-md-4">
            <h1>Historia</h1>
                </div>
    
                <div style="padding:0 20px"/>
    
                    <table class="table table-striped">
                            <tr>
                                <th>ID</th>
                                <th>Data Wyjazdu</th>
                                <th>Data Przyjazdu</th>
                                <th>Akcja</th>
                            </tr>  
                    <tr th:each = "calc : ${calcResults}">
                            <td th:text="${calc.id}"></td>
                            <td th:text="${#dates.format(calc.dataWyjazdu, 'dd-MM-yyyy')}"></td>
                            <td th:text="${#dates.format(calc.dataPrzyjazdu, 'dd-MM-yyyy')}"></td>
    
                            <td>
                                <button type="submit" class="btn btn-success btn-xs">Przelicz</button>
                                <!--<a th:href="@{/sucess}" class="btn btn-success btn-xs">Szczegóły</a>-->
                            </td>
                    </tr>
                </table>
                </form>
        </body>
    </html>
    
    <tr th:each="calc, stat : *{items}">
      <input type="text"
         th:field="*{items[__${stat.index}__].id}">
      /* other fields of the objects stored in the array has to be insert here like in the line above */
    </tr>