2014-11-05 39 views
2

我不是在春天控制器获取表单数据提交以下表单后,后是我的代码表单数据未使用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> 

在上述表单中的每个我得到的名称和说明时间为空(我认为这是创建一个新的视图对象,而不在形式给出的值)

请让我知道,在这里我错了

+0

在你的saveCategory方法重新排序参数,在categoryView后立即放置BindingResult,离开模型最后,或摆脱,看看会发生什么 – grid 2014-11-05 15:22:25

+0

按照你的说法试过,但没有解决。同样的问题 – user3682520 2014-11-05 16:47:10

+0

如果在输入帖子处理程序后立即添加断点,是否填充了categoryView? – grid 2014-11-05 16:50:41

回答

3

form标签取下enctype="multipart/form-data",然后再试一次(以正确的顺序方法参数)。 @ModelAttribute不是严格要求的,因为您的属性名称与类名匹配。

+0

惊人的....它的工作..想知道为什么数据没有约束如果enctype在那里? – user3682520 2014-11-06 08:59:19

+0

因为它期望多部分数据。例如,在上传文件时使用的是不同的类型。见http://stackoverflow.com/questions/1342506/why-is-form-encrtype-multipart-form-data-required-when-uploading-a-file – grid 2014-11-06 09:02:38

0

我觉得@ModelAttribute在CategoryView上缺少注释。因为按照您的表单代码,它是将数据绑定到控制器中的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"; 
} 
+0

我也以这种方式尝试,但没有用,也在SessionAttribute中添加categoryView – user3682520 2014-11-06 06:04:41

相关问题