【发布时间】: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="<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 的控制台中也没有显示异常。
编辑:
我忘了说控制器在类级别有RequestMapping("/users")。
现在我把控制器value = "/update"改成了value = "/updateImage",但是我在jsp页面里留下了action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />",还是报错:
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吗? -
没有。我的问题是错误的。我对代码有误解
-
通常
RequestParam与GET请求一起使用,也许这就是它不起作用的原因
标签: java spring spring-mvc