Spring mvc org.springframework.web.util.NestedServletException:请求处理失败

Spring mvc org.springframework.web.util.NestedServletException:请求处理失败,spring-mvc,jdbctemplate,spring-annotations,Spring Mvc,Jdbctemplate,Spring Annotations,我正在使用SpringJDBC模板进行CRUD。 插入、选择和删除操作工作正常,但我在更新过程中遇到以下异常 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.la

我正在使用SpringJDBC模板进行CRUD。 插入、选择和删除操作工作正常,但我在更新过程中遇到以下异常

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>()
我也为控制器使用了
@Autowired
注释。
如何解决?非常感谢您提供的任何帮助。

我看到您正在尝试使用整数companyId作为ModelAttribute。在这种情况下,我不推荐使用ModelAttribute(因为这太过分了,很容易被误用),但如果您使用,您以前声明过该ModelAttribute的值吗

public String save(@ModelAttribute("company")Integer companyId,Company company,BindingResult result, ModelMap map) {
如果仅指定上述值,系统将尝试为所有请求初始化一个整数。这无法完成,因为类Integer没有默认的讲师

因此,我建议这样做:

public String save(@RequestParam("company")Integer companyId,Company company,BindingResult result, ModelMap map) {
如果仍要对所有请求使用共享ModelAttribute,则必须首先初始化它:

@ModelAttribute("company")
public Integer companyId(){
    return 0;
}

我把我的Post方法改为following,效果很好

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

您的URL不能以不同方式重复

  • 您必须确定URL
  • @modeldattribute(“公司”)公司
  • 注意这一点,所有companyId中都将有0,这对crud来说可能是危险的

    你能用吗

    @PathVariable(value="companyId")
    

    编辑必须像保存一样唯一的更改是公司id如果要保存,则必须为0或null,如果要编辑,则必须为公司id

    ,但使用@Valid时会出现错误“注释类型Valid的属性值未定义”抱歉,我忘了@Valid仅用于对象。无论如何,您不需要验证整数。“@RequestParam”就足够了。它之所以有效,是因为您将ModelAttribute更改为公司对象。但是,我建议您阅读更多内容,以了解ModelAttribute()的基础知识。简而言之,如果在一个请求中只需要此属性,那么使用ModelAttribute是一种浪费。
    @ModelAttribute("company")
    public Integer companyId(){
    return 0;
    }
    
    @PathVariable(value="companyId")