Spring 百里香不';t绑定弹簧&x27;s模型和视图
我有一个这样的控制器:Spring 百里香不';t绑定弹簧&x27;s模型和视图,spring,spring-mvc,thymeleaf,Spring,Spring Mvc,Thymeleaf,我有一个这样的控制器: @Controller public class HelloController { @Autowired private SomeService someService; @RequestMapping("/") public ModelAndView index() { ModelAndView mav = new ModelAndView("index"); mav.addObject("title", someService.getTitle(
@Controller
public class HelloController {
@Autowired
private SomeService someService;
@RequestMapping("/")
public ModelAndView index() {
ModelAndView mav = new ModelAndView("index");
mav.addObject("title", someService.getTitle());
mav.addObject("text", someService.getText());
return mav;
}
}
现在,thymeleaf应该对mav有一些了解:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>${title}</h1>
<h2>${text}</h2>
</body>
</html>
有什么想法吗?我是否缺少有关Spring中ViewResolver的某些配置?已修复:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title th:text="${title}"></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1 th:text="${title}"></h1>
<h2 th:text="${text}">My Dummy Data</h2>
</body>
</html>
我的虚拟数据
我意识到这是一个4年前的问题,Stefano的答案是正确的。但这也是正确的(内联):
${title}
[${title}]]
[${text}]]
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title th:text="${title}"></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1 th:text="${title}"></h1>
<h2 th:text="${text}">My Dummy Data</h2>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>[[${title}]]</h1>
<h2>[[${text}]]</h2>
</body>
</html>