2

I am doing CRUD using spring jdbc template. insert,select and delete operations are working fine but I got these following exception in update process.

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.lang.Integer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Integer.<init>()

here is my controller:

@RequestMapping(value="/editCompany/{companyId}", method= RequestMethod.GET)
    public String edit(@PathVariable(value="companyId")Integer companyId,ModelMap map) {

        Company company=companyService.get(companyId);
        map.addAttribute("company", company);
        map.put("companyId", companyId);
        return "editCompany"; 
    }

    @RequestMapping(value="/editCompany/{companyId}", method= RequestMethod.POST)
        public String save(@ModelAttribute("company")Integer companyId,Company company,BindingResult result, ModelMap map) {

        companyValidator.validate(company, result);
        if (result.hasErrors()) {
            return "editCompany";
        } else {
            Integer i=companyService.save(company);

            return "status";
        }
    }

I have used @Autowired annotation for the controller too. How to resolve it? any kind of help is appreciated.

smya.dsh
  • 651
  • 5
  • 10
  • 23

4 Answers4

3

I see that you are trying to use an Integer companyId as a ModelAttribute. I won't recommend ModelAttribute for this case (since it's overkill & easy to misuse), but in case you use, have you declare the value of that ModelAttribute before?

public String save(@ModelAttribute("company")Integer companyId,Company company,BindingResult result, ModelMap map) {

If you only specify the value like above, the system will try to initialize an Integer for all the requests. This can't be complete because class Integer doesn't have a default instructor.

Hence I recommend doing it like this:

public String save(@RequestParam("company")Integer companyId,Company company,BindingResult result, ModelMap map) {

If you still want to use a shared ModelAttribute for all your request, you must intialize it first:

@ModelAttribute("company")
public Integer companyId(){
    return 0;
}
Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
  • but using @Valid it's giving an error "The attribute value is undefined for the annotation type Valid" – smya.dsh Jan 16 '13 at 09:48
  • 1
    sorry, I forgot. @Valid is only used for Object. Anyway, you won't need to validate an Integer. "@RequestParam" is enough. – Hoàng Long Jan 16 '13 at 16:06
1

I changed my Post method to following and it worked.

public String save(@ModelAttribute("company")Company company,BindingResult result, ModelMap map)
smya.dsh
  • 651
  • 5
  • 10
  • 23
  • 1
    It works because that you changed your ModelAttribute to a Company object. However, I would recommend reading some more to understand the basics of ModelAttribute (http://stackoverflow.com/questions/3423262/what-is-modelattribute-in-spring-mvc). In short, if you only need this attribute in one request, then using ModelAttribute is a waste. – Hoàng Long Jan 16 '13 at 16:08
0
@ModelAttribute("company")
public Integer companyId(){
return 0;
}

beware with this, you will have 0 in all companyId and this could be dangerous for crud.

¿can you use?

@PathVariable(value="companyId")

edit must be like save the only change is the companyId if you are saving must be 0 or null and if you are editing must be the id of the company

manuel
  • 17
  • 3
-3

Your URL cannot be repeated differently.

  1. you must be sure of the URL
  2. @ModelAttribute ("company") company
David Cain
  • 16,484
  • 14
  • 65
  • 75
andy
  • 1