【发布时间】:2014-06-04 12:50:16
【问题描述】:
我想在同一个控制器中创建不同的@Entity 实体。
public class ContentContainer {
public Object content;
public ContentContainer(){}
public ContentContainer(Object object){
setContent(object);
}
public Object getContent() {
return content;
}
public void setContent(Object content) {
this.content = content;
}
}
控制器方法
@RequestMapping(value="create", method=RequestMethod.GET)
public String GET(Model model) throws InstantiationException, IllegalAccessException {
Class<?> clazz = ????; // a Random POJO is chosen, i want to use POJOs!!
Object object = clazz.newInstance();
model.addAttribute("mymodel", new ContentContainer(object));
return "create";
}
@RequestMapping(value="create", method=RequestMethod.POST)
public @ResponseBody Object POST(@ModelAttribute(value="mymodel") ContentContainer cc) {
Object object = cc.getContent();
// object == NULL but why??
return object;
}
Thymeleaf 页面,正如另一条评论中所要求的那样。 这就是我发布这个的原因 多一点 文本 所以我可以发帖 此附加信息 不知道 为什么这是必要的。 请原谅我。 但这必须做 遵守规则。
<head>
<title th:text="${title}">Entity create</title>
</head>
<body>
<form action="#" th:object="${mymodel}" th:action="@{${url}}" method="post">
<fieldset>
<table class="table table-striped">
<thead>
<tr>
<th style="width: 150px;">Fieldname</th>
<th style="width: 150px;">Input</th>
</tr>
</thead>
<tbody>
<tr th:each="v : ${values}">
<td><label th:for="${v}" th:text="${v}">Fieldname: </label></td>
<td><input type="text" th:field="*{__${v}__}" /></td>
</tr>
</tbody>
</table>
<div class="submit">
<button type="submit" name="create" th:text="#{entity.create}">Create</button>
</div>
</fieldset>
</form>
</body>
</html>
在 Post 方法中,我得到 NULL 的 getContent
我错过了什么?
【问题讨论】:
-
和你的其他问题一样,这里是stackoverflow.com/questions/24031858/…
-
但是我怎样才能实现它,我可以多次打开同一个页面而不会在不同的站点请求之间交换或共享会话? (如果我打开多个标签到同一个 url,但我会将不同的参数传递给 url)
-
请阅读我给出的2个解决方案的答案!
-
@ModelAttribute 也不起作用,如果我只使用 Session 将 pojo 的更改发送到控制器,我不喜欢处理它
-
为什么这不起作用,该方法在每个方法之前执行,如果命名属性可用,则应该使用它。
标签: spring model controller