我有实体:
@Entity
public class News
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String content;
private boolean isMain;
}
按下编辑按钮后,我可以按表格编辑标题和内容:
@RequestMapping(value = "/edit/{newsId}", method = GET)
public String edit(@PathVariable long newsId, Model model)
{
News news = newsService.getById(newsId); //hibernate
model.addAttribute("news", news);
return "admin";
}
形式:
<form method = "POST" th:object = "${news}" th:action="@{/news/editNews}" id = "add_news_form">
<input type = "text" th:field = "*{title}" th:value="*{title}" />
<input type = "submit" value = "Edit" /><br>
<textarea name = "news_content" rows = "20" cols = "80" th:field = "*{content}" th:text= "*{content}" form = "add_news_form" >Wpisz... </textarea>
</form>
和POST方法:
@RequestMapping(value = "/editNews", method = POST)
public String editNews( News news )
{
newsService.update(news); //hibernate
return "redirect:/";
}
它给出了null异常,因为post方法中的news对象只有title和content字段为null。我可以添加文本输入来设置id,但是用户不能这样做。如果我有100个其他字段并且我想在表单中保存并且只更改其中的两个字段怎么办?
答案 0 :(得分:0)
在这种情况下,我可能必须使用RedirectModel吗?