您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

错误:java.lang.NoSuchMethodException:java.lang.Long。()在春季MVC中

错误:java.lang.NoSuchMethodException:java.lang.Long。()在春季MVC中

@modelattribute("studentId") Long studentId是问题的根源,因为spring找不到可以提供此Long对象的方法,因此它试图实例化一个对象并将其作为方法参数传递。 您可以:

        @RequestMapping(value = "/read.html")
    public String readStudent(Model model,Long studentId) {
        Student student = null;
        studentId = 2l;
        try {
            student = serviceFile.readStudent(studentId);
        } catch(Exception e){
            model.addAttribute("message", "Some thing went wrong !!!! Exception occured");
            return "message";
        }
        model.addAttribute("student", student);
        return "read";
    }
        @modelattribute
    public void provideStudentId(Model model){
        model.addAttribute("studentId", new Long(1));
    }

官方文件

    @RequestMapping(path = "/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
    public String processSubmit(@modelattribute Pet pet) { }

给定以上示例,Pet实例可以从哪里来?有几种选择:

如果studentId是参数的名字从你可以使用UI发送@RequestParam这样

    @RequestMapping(value = "/read.html")
    public String readStudent(Model model, @RequestParam("studentId") Long studentId) {
        Student student = null;
        studentId = 2l;
        try {
            student = serviceFile.readStudent(studentId);
        } catch(Exception e) {
            model.addAttribute("message", "Some thing went wrong !!!! Exception occoured");
            return "message";
        }   
        model.addAttribute("student", student);
        return "read";
    }
java 2022/1/1 18:17:45 有456人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶