栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

  • 从方法参数中删除@ModelAttribue

    @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”;
    }

  • 创建将

    Long
    在您的 控件提供该 对象 的方法

    @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实例可以从哪里来?有几种选择:

  1. 由于使用@SessionAttributes,它可能已经在模型中-
    请参阅“使用@SessionAttributes在请求之间的HTTP会话中存储模型属性”一节。
  2. 由于同一控制器中的@ModelAttribute方法,它可能已经在模型中-如上一节中所述。
  3. 可以基于URI模板变量和类型转换器(在下面更详细地说明)来检索它。
  4. 可以使用其默认构造函数实例化它。

编辑
如果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";}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390900.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号