【问题标题】:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long' error when submitting form提交表单时无法将类型“java.lang.String”的值转换为所需类型“java.lang.Long”错误
【发布时间】:2020-05-24 17:48:24
【问题描述】:

我在控制器中有这个:

@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文件中:

<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>

当我提交图像时,我得到了

Whitelabel 错误页面 此应用程序没有明确的映射 /error,因此您将其视为后备。

2020 年 2 月 8 日星期六 19:08:10 CST 出现意外错误 (type=Bad 请求,状态=400)。类型值转换失败 'java.lang.String' 到所需类型'java.lang.Long';嵌套异常 是 java.lang.NumberFormatException:对于输入字符串:“更新”

字符串"controller executed!" 永远不会出现在控制台中。我在 RequestMapping 注释中将value = "/update" 更改为value = "/updateImage",将action="&lt;c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' /&gt;" 更改为action="&lt;c:url value='/users/updateImage?${_csrf.parameterName}=${_csrf.token}' /&gt;",并将错误消息更改为nested exception is java.lang.NumberFormatException: For input string: "updateImage"

我不知道出了什么问题,在 Eclipse 的控制台中也没有显示异常。

编辑: 我忘了说控制器在类级别有RequestMapping("/users")

现在我把控制器value = "/update"改成了value = "/updateImage",但是我在jsp页面里留下了action="&lt;c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' /&gt;",还是报错:

Whitelabel 错误页面 此应用程序没有明确的映射 /error,因此您将其视为后备。

2020 年 2 月 8 日星期六 19:08:10 CST 出现意外错误 (type=Bad 请求,状态=400)。类型值转换失败 'java.lang.String' 到所需类型'java.lang.Long';嵌套异常 是 java.lang.NumberFormatException:对于输入字符串:“更新”

我猜这个请求甚至没有到达控制器。

【问题讨论】:

  • @R.G 你的意思是jsp正在尝试将字符串'/users/update?${_csrf.parameterName}=${_csrf.token}'转换为Long吗?
  • 没有。我的问题是错误的。我对代码有误解
  • 通常RequestParamGET 请求一起使用,也许这就是它不起作用的原因

标签: java spring spring-mvc


【解决方案1】:

我终于通过从@RequestMapping 中删除params 属性来解决问题。因此,控制器最终成为:

    @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");
    }

我在其他方法中将params 属性与发布请求一起使用,并且它有效,我猜这里的问题与请求是多部分请求有关。但我真的不知道实际问题是什么。

【讨论】:

    猜你喜欢
    • 2019-09-05
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2016-02-18
    • 2021-09-02
    • 2020-07-02
    相关资源
    最近更新 更多