我在 Controller 中有这个:
@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 Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Feb 08 19:08:10 CST 2020 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "update"
字符串"controller executed!"
永远不会出现在控制台中。我变了value = "/update"
通过value = "/updateImage"
在 RequestMapping 注释和 action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />"
中通过action="<c:url value='/users/updateImage?${_csrf.parameterName}=${_csrf.token}' />"
错误消息更改为 nested exception is java.lang.NumberFormatException: For input string: "updateImage"
我不知道出了什么问题,并且在 Eclipse 的控制台中没有显示异常。
编辑:
我忘了说 Controller 有 RequestMapping("/users")
在类(class)层面。
现在我在 Controller 中进行了更改value = "/update"
通过value = "/updateImage"
,但我离开了action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />"
在jsp页面中,错误仍然是:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Feb 08 19:08:10 CST 2020 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "update"
我猜请求甚至没有到达 Controller 。
最佳答案
我最终通过从 @RequestMapping
中删除 params
属性解决了这个问题。因此, Controller 最终为:
@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
属性和 post 请求,并且它有效,我想这里的问题与请求是多部分请求有关。但我真的不知道真正的问题是什么。
关于java - 提交表单时无法将类型 'java.lang.String' 的值转换为所需类型 'java.lang.Long' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60132960/