我不是在春天控制器獲取表單數據提交以下表單後,後是我的代碼表單數據未使用Spring控制器註釋獲得新的對象綁定提交
@RequestMapping(value = "category/addCategory.htm" , method = RequestMethod.GET)
public String add(Model model) {
if (log.isDebugEnabled()){
log.debug("Invoking listCategory");
}
model.addAttribute("categoryView", new CategoryView());
return "editCategory";
}
@RequestMapping(value = "category/saveCategory.htm", method = RequestMethod.POST)
public String saveCategory(CategoryView categoryView, Model model, BindingResult result) {
Category category = prepareCategoryFromView(categoryView);
categoryService.save(category);
categoryView.setCategoryId(category.getCategoryId());
model.addAttribute("categoryView",categoryView);
return "editCategory";
}
prepareCategoryFromView是被設定的實際值的方法在類別它的休眠實體,以下類別查看
public class CategoryView {
private long categoryId;
private String image = "";
private int parentId;
private boolean top;
private int column = 1;
private int sortOrder = 1;
private boolean status;
private String description;
private String name;
.
.
other variable and setter and getters
}
和形式是
<sf:form method="post" enctype="multipart/form-data" id="form-category" cssClass="form-horizontal" modelAttribute="categoryView">
<sf:label path="name" cssClass="col-sm-2 control-label">Category Name</sf:label>
<sf:input path="name" id="name" name="name" cssClass="form-control" placeholder="Category Name" />
<sf:hidden path="categoryId" id="categoryId" name="categoryId" />
<sf:hidden path="languageId" id="languageId" name="languageId" />
<sf:label path="description" cssClass="col-sm-2 control-label">Category Name</sf:label>
<sf:textarea path="description" cssClass="form-control" placeholder="Description" id="description"/>
.
.
.
</sf:form>
在上述表單中的每個我得到的名稱和說明時間爲空(我認爲這是創建一個新的視圖對象,而不在形式給出的值)
請讓我知道,在這裏我錯了
在你的saveCategory方法重新排序參數,在categoryView後立即放置BindingResult,離開模型最後,或擺脫,看看會發生什麼 – grid 2014-11-05 15:22:25
按照你的說法試過,但沒有解決。同樣的問題 – user3682520 2014-11-05 16:47:10
如果在輸入帖子處理程序後立即添加斷點,是否填充了categoryView? – grid 2014-11-05 16:50:41