【发布时间】:2013-01-09 00:00:05
【问题描述】:
我正在开发一个使用 Spring MVC 和 Thymeleaf 作为我的 ViewResolver 的 Web 应用程序。我有以下控制器处理程序方法:
@RequestMapping(value = "/something", method = RequestMethod.POST, params = "submit")
public String doSomething(@ModelAttribute("error") String error /*, other attributes */) {
// find out if there is an error
error = getErrorMessage();
return "someHTMLfile";
}
我的视图包含这一行:
<p><span th:text="${error}">Error Message goes here</span></p>
执行时,标签不会呈现给任何东西。这可能是由于 ${error} 评估为空字符串,但我不明白为什么。 Spring的@ModelAttribute注解不是自动将对象添加到模型图中,Thymeleaf在哪里可以找到?
如果我有:
@RequestMapping(value = "/something", method = RequestMethod.POST, params = "submit")
public String doSomething(ModelMap map /*, other attributes */) {
// find out if there is an error
String error;
error = getErrorMessage();
map.addAttribute("error", error);
return "someHTMLfile";
}
显示错误消息的视图完美无缺。 @ModelAttribute 不会将对象添加到请求模型中吗?
编辑:我都试过了:
@RequestMapping(value = "/something", method = RequestMethod.POST, params = "submit")
public String doSomething(@ModelAttribute("error") String error, ModelMap map /*, other attributes */) {
// find out if there is an error
error = getErrorMessage();
map.addAttribute("error", error);
return "someHTMLfile";
}
这也行不通。
【问题讨论】:
标签: java html spring-mvc thymeleaf