【问题标题】:How to represent 2 model objects inside the thymeleaf template如何在 thymeleaf 模板中表示 2 个模型对象
【发布时间】:2016-09-12 00:19:06
【问题描述】:

我在这个spring-boot 项目中工作,我正在从我的controller 方法返回一个ModelAndView 对象,我已经向ModelAndView 添加了2 个对象。这部分正在工作,我想知道如何表示 thymeleaf 模板中的值。

public ModelAndView showEdit(@PathVariable int id,Customer cust,Model model){
    ModelAndView view  = new ModelAndView();
    view.setViewName("editCustom");
    view.addObject("cust",cust);
    view.addObject("log",login);
}

thymeleaf 模板内。

<form action="#" th:action="@{/save}" th:object="${cust}" method="post">
 Name:<input type="text" th:field="*{name}" /> 

我可以在cust 中获取值,但我不知道如何从login 中获取值。 我试过了,但它不起作用。注意所有输入标签都在同一个表单内。

<input type="text" id="user" name="user" value="${login.uname}"/>

【问题讨论】:

    标签: spring jsp spring-mvc spring-boot thymeleaf


    【解决方案1】:

    在您的模型中,您将登录详细信息添加为日志,而在您的视图中,您正在使用登录

    view.addObject("log",login);

    ${login.uname}

    thymeleaf 也使用属性处理器来处理前缀为 th 的属性。而不是使用 value 使用 th:value 如下

    <input type="text" id="user" name="user" th:value="${log.uname}"/>
    

    【讨论】:

      猜你喜欢
      • 2017-10-15
      • 2013-08-27
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      相关资源
      最近更新 更多