我可以在使用@ModelAttribute注释的方法中将Spring模型用作模型吗?我收到错误消息,说它无法在模型中找到该属性。
以下是将对象添加到模型的方法:
@ModelAttribute("survivor")
public Model getSurvivors(Model m) {
m.addAttribute("decedent", new Decedent());
return m;
}
这是渲染方法。它打印'model contains decedent = true'
@RenderMapping(params="render=survivors")
public String showSurvivors(Model model, RenderRequest request, RenderResponse response) throws Exception {
logger.info("in showSurvivors");
logger.debug("model contains decedent={}, mode.containsAttribute("decedent");
return "survivors";
}
这是jsp:
<form:form commandName="survivor" action="${submitAction}">
<form:input path="decedent.firstName" />
错误:
org.springframework.web.servlet.tags.RequestContextAwareTag doStartTag bean类的属性'decedent'无效 [org.springframework.validation.support.BindingAwareModelMap]:Bean property'decedent'无法读取或具有无效的getter方法: getter的返回类型是否与参数类型匹配 设定器?
答案 0 :(得分:1)
你实质上是这样做的:
model.addAttribute("survivor", model);
您在form:input
中看到的问题是,它期望模型上的getter decedent
不存在。修复可能是在普通Map上使用另一个包装器类型:
public class MyCommand{
private Map<String, Decedent> decedents;
...getters and setters.
}
...
model.addAttribute("survivor", ..); //Add MyCommand type..
...
<form:form commandName="survivor" action="${submitAction}">
<form:input path="decedents['decedent']" />