首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提交表单时未能将“java.lang.String”类型的值转换为所需类型“java.lang.Long”错误

提交表单时未能将“java.lang.String”类型的值转换为所需类型“java.lang.Long”错误
EN

Stack Overflow用户
提问于 2020-02-09 02:17:21
回答 1查看 751关注 0票数 0

我在监狱长那里有这个:

代码语言:javascript
复制
@RequestMapping(
            value = "/update",
            params = {"new_profile_image"},
            method = RequestMethod.POST)
    public ModelAndView updateUserProfileImage(
            @RequestParam(value = "new_profile_image") CommonsMultipartFile newProfileImage,
            ModelMap model) {

        System.out.println("controller executed!");

        if(newProfileImage != null && !newProfileImage.isEmpty()) {
            updateService.updateUserProfileImage(newProfileImage.getBytes());
        }

        return new ModelAndView("redirect:/users/my_profile");
    }

在jsp文件中:

代码语言:javascript
复制
<form action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />" method="post"  enctype="multipart/form-data">
    <input name="new_profile_image" type="file" id="new_profile_image">

    <button type="submit" class="btn btn-primary">Update</button>
</form>

当我提交一张图片时

白线错误页此应用程序没有/error的显式映射,因此您认为这是一种退步。

2月8日(星期六) 19: 08 :10 CST 2020有一个意外的错误(type=Bad请求,status=400)。未能将“java.lang.String”类型的值转换为所需的类型'java.lang.Long';嵌套异常为java.lang.NumberFormatException: For input string:"update“

"controller executed!"字符串从未出现在控制台中。我在value = "/update"注释中通过value = "/updateImage"更改了action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />",在action="<c:url value='/users/updateImage?${_csrf.parameterName}=${_csrf.token}' />"中更改了action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />",并将错误消息编码为nested exception is java.lang.NumberFormatException: For input string: "updateImage"

我不知道出了什么问题,在eclipse的控制台中也没有显示任何例外。

编辑:我忘记说控制器具有类级别的RequestMapping("/users")

现在,我用value = "/updateImage"更改了控制器value = "/updateImage",但是在jsp页面中保留了action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />",错误仍然是:

白线错误页此应用程序没有/error的显式映射,因此您认为这是一种退步。

2月8日(星期六) 19: 08 :10 CST 2020有一个意外的错误(type=Bad请求,status=400)。未能将“java.lang.String”类型的值转换为所需的类型'java.lang.Long';嵌套异常为java.lang.NumberFormatException: For input string:"update“

我想请求甚至没有到达控制器。

EN

回答 1

Stack Overflow用户

发布于 2020-02-09 22:58:28

我最终通过从params中删除@RequestMapping属性来解决这个问题。因此,控制器的结尾是:

代码语言:javascript
复制
    @RequestMapping(
            value = "/updateImage",
            method = RequestMethod.POST)
    public ModelAndView updateUserProfileImage(
            @RequestParam(value = "new_profile_image", required = true) CommonsMultipartFile newProfileImage,
            ModelMap model) {

        System.out.println("controller executed!");

        if(newProfileImage != null && !newProfileImage.isEmpty()) {
            updateService.updateUserProfileImage(newProfileImage.getBytes());
        }

        return new ModelAndView("redirect:/users/my_profile");
    }

我在其他方法中使用了post请求的params属性,而且它可以工作,我猜这里的问题与请求是多部分请求有关。但我不知道真正的问题是什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60132960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档