presidentrodel
Jun 10th, 2011, 04:04 AM
Hello developers,
I am having an error during my file upload app execution. This is my error:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBinding Result: 1 errors
Field error in object 'product' on field 'productImage': rejected value [org.springframework.web.multipart.commons.CommonsM ultipartFile@151c368]; codes [typeMismatch.product.productImage,typeMismatch.pro ductImage,typeMismatch.[B,typeMismatch]; arguments [org.springframework.context.support.DefaultMessage SourceResolvable: codes [product.productImage,productImage]; arguments []; default message [productImage]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.commons.Commons MultipartFile' to required type 'byte[]' for property 'productImage'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsM ultipartFile] to required type [byte] for property 'productImage[0]': PropertyEditor [org.springframework.beans.propertyeditors.CustomNu mberEditor] returned inappropriate value]
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.doBind(HandlerMethodInvoker.jav a:820)
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.resolveHandlerArguments(Handler MethodInvoker.java:359)
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:427)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:415)
org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:788)
org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:717)
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet .java:641)
javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
this is my entity class:
@Entity
public class Product {
@Id
@GeneratedValue
private Long id;
private byte[] productImage;
private String name;
private String description;
public byte[] getProductImage() {
return productImage;
}
public void setProductImage(byte[] productImage) {
this.productImage = productImage;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
and this is my controller:
@Controller
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductDAO productDAO;
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
@SuppressWarnings("unused")
@RequestMapping(method = RequestMethod.GET)
private String showProducts(Model model) {
List<Product> listOfAllProducts = productDAO.findAll();
model.addAttribute("listOfProducts", listOfAllProducts);
return "productHome";
}
@SuppressWarnings("unused")
@RequestMapping(value = "/addProduct", method = RequestMethod.GET)
private String addProduct(@ModelAttribute Product product) {
return "addProductPage";
}
@SuppressWarnings("unused")
@RequestMapping(value = "/addProduct", method = RequestMethod.POST)
private String addProductSuccess(@ModelAttribute Product product) {
productDAO.saveProduct(product);
return "redirect:/products";
}
}
Please help me with this problem.
Thanks.
I am having an error during my file upload app execution. This is my error:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBinding Result: 1 errors
Field error in object 'product' on field 'productImage': rejected value [org.springframework.web.multipart.commons.CommonsM ultipartFile@151c368]; codes [typeMismatch.product.productImage,typeMismatch.pro ductImage,typeMismatch.[B,typeMismatch]; arguments [org.springframework.context.support.DefaultMessage SourceResolvable: codes [product.productImage,productImage]; arguments []; default message [productImage]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.commons.Commons MultipartFile' to required type 'byte[]' for property 'productImage'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsM ultipartFile] to required type [byte] for property 'productImage[0]': PropertyEditor [org.springframework.beans.propertyeditors.CustomNu mberEditor] returned inappropriate value]
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.doBind(HandlerMethodInvoker.jav a:820)
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.resolveHandlerArguments(Handler MethodInvoker.java:359)
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:427)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:415)
org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:788)
org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:717)
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet .java:641)
javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
this is my entity class:
@Entity
public class Product {
@Id
@GeneratedValue
private Long id;
private byte[] productImage;
private String name;
private String description;
public byte[] getProductImage() {
return productImage;
}
public void setProductImage(byte[] productImage) {
this.productImage = productImage;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
and this is my controller:
@Controller
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductDAO productDAO;
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
@SuppressWarnings("unused")
@RequestMapping(method = RequestMethod.GET)
private String showProducts(Model model) {
List<Product> listOfAllProducts = productDAO.findAll();
model.addAttribute("listOfProducts", listOfAllProducts);
return "productHome";
}
@SuppressWarnings("unused")
@RequestMapping(value = "/addProduct", method = RequestMethod.GET)
private String addProduct(@ModelAttribute Product product) {
return "addProductPage";
}
@SuppressWarnings("unused")
@RequestMapping(value = "/addProduct", method = RequestMethod.POST)
private String addProductSuccess(@ModelAttribute Product product) {
productDAO.saveProduct(product);
return "redirect:/products";
}
}
Please help me with this problem.
Thanks.