假设我有一个类 Factory
有 2 个字段:fName
和fArea
。是否可以通过单个对象引用这些字段f1
( Factory
的实例)添加到 Model
或ModelAndView
?结果是这样的${f.fArea}
(其中 f
是 String attributeName
方法的 addObject
参数)。
最佳答案
是的,这是可能的。该语法是 thymeleaf 表达式工作方式的基础...
// Controller
@GetMapping("/whatever")
public String whatever(Map<String, Object> model) {
model.put("f", new Factory());
return "whatever";
}
// Template
</span th:text="${f.fArea}" />
http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#variables
关于java - 直接访问对象属性 thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42862502/