【问题标题】:Form data not binding with Spring Controller Annotation getting new object after submit提交后表单数据未与 Spring Controller Annotation 绑定获取新对象
【发布时间】: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


【解决方案1】:

从您的form 标记中删除enctype="multipart/form-data",然后重试(使用正确顺序的方法参数)。 @ModelAttribute 不是严格要求的,因为您的属性名称与类名称匹配。

【讨论】:

  • Amazing.... 成功了.. 想知道如果 enctype 存在,为什么数据没有绑定?
  • 因为它需要多部分数据。它是一种不同的类型,例如在您上传文件时使用。见stackoverflow.com/questions/1342506/…
【解决方案2】:

我认为 CategoryView 对象上缺少 @ModelAttribute 注释。因为根据您的表单代码,它是将数据绑定到控制器中的 bean 的模型属性。

将其与您的方法参数附加如下,然后您可以检查数据是否绑定到它。

@RequestMapping(value = "category/saveCategory.htm", method = RequestMethod.POST)
public String saveCategory(@ModelAttribute("categoryView") CategoryView categoryView, Model model,  BindingResult result) {

    Category category = prepareCategoryFromView(categoryView);

    categoryService.save(category);
    categoryView.setCategoryId(category.getCategoryId());
    model.addAttribute("categoryView",categoryView);
    return "editCategory";
}

【讨论】:

  • 我也试过这种方式但没有用,我还在SessionAttribute中添加了categoryView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
相关资源
最近更新 更多