我有很多带有此代码的 Controller :
@RequestMapping("/test1")
public String test (@ModelAttribute("page") Page page) {
...
}
@RequestMapping("/test2")
public String test2 (@ModelAttribute("page") Page page) {
...
}
是否可以在 Spring (4.3) 中创建一个元注释来缩短此注释,如下所示:
@RequestMapping("/test3")
public String test2 (@MyCustom Page page) {
...
}
那么 @ModelAttribute("page") 将变成 @MyCustom?
最佳答案
不幸的是,这行不通。 As you can see in the sources , ModelAttribute
已注释
@Target({ElementType.PARAMETER, ElementType.METHOD})
which means it can be applied to methods and method parameters only 。对于元注释,它必须应用于类型级别。
参见Spring Annotation Programming Model了解详细信息,但基本上这是元注释不起作用的边缘情况。
关于java - Spring:为@ModelAttribute创建元注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38055980/