user3682520
user3682520

Reputation: 71

Form data not binding with Spring Controller Annotation getting new object after submit

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

Upvotes: 2

Views: 8106

Answers (2)

gregdim
gregdim

Reputation: 2071

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.

Upvotes: 4

dReAmEr
dReAmEr

Reputation: 7196

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";
}

Upvotes: 0

Related Questions