春天百里香没有遍历列表

时间:2019-08-19 08:51:31

标签: java spring loops model-view-controller thymeleaf

我试图遍历一个List并在一个表中显示结果,但是该表保持为空。

控制器

def runner(address=None):
    # Or maybe just runner(address) if you don't want to make the
    # address argument optional

home.jsp-表

if __name__ == '__main__':
    address = sys.argv[1]  # Use argparse instead if you can
    runner(address=address)

OrderNotify.java

@RequestMapping(value="/home")
    public String home(Model model) throws IOException {
        List<OrderNotify> notifications = new ArrayList<OrderNotify>();
        for (int i = 0; i < 10; i++) {
            OrderNotify notification = new OrderNotify("1", "2", "3", "4");
            notifications.add(notification);


        }
            model.addAttribute("notifications", notifications);
            return "home";
    }

我希望看到10行具有与列表的OrderNotify对象相同的信息,但是它为空。我在浏览器中查看了源代码,并得到以下结果:

<table id="handle" style=" margin:auto">

    <tr>
        <th>Bestellnummer</th>
        <th>Datum</th>
        <th>Status</th>
        <th>Handlung erforderlich</th>
    </tr>
    <tr th:each="notify : ${notifications}">
        <td th:text="${notify.orderid}"></td>
        <td th:text="${notify.date}"></td>
        <td th:text="${notify.status}"></td>
        <td th:text="${notify.handle}"> </td>
    </tr>



</table>

1 个答案:

答案 0 :(得分:0)

Thymeleaf和JSP是两个不同的服务器端渲染模板。没有配置,您将无法同时使用它们。

基于代码,您应该更新使用Thymeleaf的设置:
一种。将home.jsp更改为home.html
b。将home.html移至路径:src / main / resources / templates
C。在application.properties中配置Thymeleaf:

$registrant = $this->RmrEventsRegistrant->find('all', array('conditions' =>
    array(
        array('RmrEventsRegistrant.event_id' => $event_ids)
   ),
   'fields' => array('RmrEventsRegistrant.id,RmrEventsRegistrant.first_name,RmrEventsRegistrant.last_name,RmrEventsRegistrant.email,RmrEventsRegistrant.event_id'),
   'order' => array('RmrEventsRegistrant.created'),
));
d。 pom.xml中的config Thymeleaf依赖项

   # Thymeleaf
   spring.thymeleaf.prefix=classpath:/templates/
   spring.thymeleaf.suffix=.html
   spring.thymeleaf.encoding=UTF-8
   spring.thymeleaf.mode=HTML5
   spring.thymeleaf.cache=false


对于Viewing模板,SpringBoot建议使用Thymeleaf代替JSP,有关详细信息,请参见more

要在SpringBoot应用程序中同时使用Thymeleaf和JSP,您需要配置更多。以下文章供您参考:
Using both Thymeleaf and JSP
https://www.oodlestechnologies.com/blogs/Use-Thymeleaf-And-JSP-Simultaneously-In-Spring-Boot-App/