rvi*_*nca 2 java spring spring-mvc jakarta-ee
我正在尝试使用方法参数上的 @ModelAttribute 注释注入模型属性。
@RequestMapping({"/", "/index"})
public String home(Principal principal, Model model, @ModelAttribute("commerceId") Long commerceId) {
if (commerceId == null) {
LOGGER.info("Initializing commerce code...");
String name = principal.getName();
commerceId = Long.parseLong(name);
model.addAttribute("commerceId", commerceId);
}
return "index";
}
Run Code Online (Sandbox Code Playgroud)
但我总是得到下一个例外:
java.lang.NoSuchMethodException: java.lang.Long.<init>()
Run Code Online (Sandbox Code Playgroud)
我可以看到 spring 正在尝试使用无参数构造函数创建 Long 值,但显然它会失败,因为 Long 类型没有无参数构造函数。
为什么 Spring 无法使用 @ModelAttribute 正确创建 Long 类型?任何 Wrapper 类型(Integer、Long 等)都可以注入 @ModelAttribute 吗?
我正在使用 Spring 3.1.1
例如:
@ModelAttribute("peson") Person person
Run Code Online (Sandbox Code Playgroud)
首先,它尝试使用无参数构造函数创建一个 Person 对象。然后它调用 setter 方法来设置/填充各个属性的值。
的情况下
@ModelAttribute("commerceId") Long commerceId
Run Code Online (Sandbox Code Playgroud)
没有无参数构造函数,也没有设置值的 setter 方法。Primitive Wrapper 类提供 arg-base 构造函数,也没有 setter 方法。因为一旦初始化它,它就不允许您更改该值。
试试 @RequestParam("commerceId") Long commerceId
| 归档时间: |
|
| 查看次数: |
3926 次 |
| 最近记录: |