【发布时间】:2018-12-09 03:16:36
【问题描述】:
提交表单后,我没有在 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 是一种在 CategoryView 下方的休眠实体中设置实际值的方法
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方法中对参数重新排序,把BindingResult紧跟在categoryView之后,把model放在最后,或者干掉,看看会发生什么
-
按你说的试过了,但没有解决。同样的问题
-
如果在 post handler 输入后立即添加断点,categoryView 是否填充?
-
是的,它正在作为新的 CategoryView 填充
标签: java spring forms spring-mvc