【发布时间】:2017-02-23 20:52:47
【问题描述】:
我怎样才能有一个接口作为ModelAttribute 在下面的场景中?
@GetMapping("/{id}")
public String get(@PathVariable String id, ModelMap map) {
map.put("entity", service.getById(id));
return "view";
}
@PostMapping("/{id}")
public String update(@ModelAttribute("entity") Entity entity) {
service.store(entity);
return "view";
}
sn-p 上面给出以下错误
BeanInstantiationException: Failed to instantiate [foo.Entity]: Specified class is an interface
我不想让spring为我实例化entity,我想使用map.put("entity", ..)提供的现有实例。
【问题讨论】:
-
你有没有在视图侧感觉
entity对象? -
百里香形式:
<form th:object="${entity}">..</form> -
好的,这是正确的,并确保您在该实体类中的属性名称与
th:field相同 -
@JohanSjöberg 实体是否存储在会话中?否则它将无法在请求/响应周期中存活。
-
@Kayaman 不,不是。很高兴知道。
标签: java spring spring-mvc model-view-controller spring-web