2

I am not getting form data in spring controller after submitting the form below is my code

@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 is a method which is setting the actual values on Category it's hibernate entity, below 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
}

and the form is

<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>

In above form every time I am getting name and description is null (I think it's creating a new view object without given values in form)

Pls let me know, where I am wrong

user3682520
  • 81
  • 3
  • 14

2 Answers2

4

Remove the enctype="multipart/form-data" from your form tag and try again (with the method arguments in the correct order). @ModelAttribute is not strictly required since your attribute name matches the class name.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
gregdim
  • 2,031
  • 13
  • 15
  • Amazing.... It's worked.. Want to know why data is not binding if enctype is there? – user3682520 Nov 06 '14 at 08:59
  • Because it expects multipart data. It is a different type which is used when you upload files for example. see http://stackoverflow.com/questions/1342506/why-is-form-encrtype-multipart-form-data-required-when-uploading-a-file – gregdim Nov 06 '14 at 09:02
0

I think @ModelAttribute annotation is missing here on CategoryView object.Because as per your form code it is the model attribute which will bind data to the bean in controller.

Attach it with your method argument like below,then you can check the data is binding to it or not.

@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";
}
dReAmEr
  • 6,986
  • 7
  • 36
  • 63