java - Spring form binding drop down object -
facing issue in spring form binding.say have 2 models category , product.
@entity @table(name="product") public class product { @id @generatedvalue private long productid; @manytoone @joincolumn(name="categoryid") private category category; //getters , setters } @entity @table(name = "category") public class category { @id @generatedvalue private long categoryid; private string categoryname; }
in controller render add product page
@requestmapping(value = "/productpage", method=requestmethod.get) private modelandview getaddproductpage(){ modelandview modelandview = new modelandview("add-product","product",new product()); map<category,string> categoriesmap = new hashmap<category, string>(); list<category> categories = categoryservice.getallcategories(); if(categories != null && !categories.isempty()) { for(category category : categories) { categoriesmap.put(category, category.getcategoryname()); } } modelandview.addobject("categorylist",categories); return modelandview; }
i able populate drop down values of categories in jsp page using below code :
<form:select path="category" > <form:options items="${categorylist}"/> </form:select>
while submitting form i'm facing error 400 request sent client syntactically incorrect.failed convert property value of type 'java.lang.string' required type 'com.example.model.category' property 'category'.
if view page source each option category assigned correctly. not understanding why spring throwing err.. need help. in advance!
you should make following change in controller's action:
for(category category : categories) { categoriesmap.put(category.gecategorytid(), category.getcategoryname()); }
and in view change:
<form:select path="category.categoryid" >
the select drop-down have category name display text , category id value.
Comments
Post a Comment