/** * Annotation that binds a method parameter or method return value * to a named model attribute, exposed to a web view. Supported * for controller classes with {@link RequestMapping @RequestMapping} * methods. * 此注解用于绑定一个方法参数或者返回值到一个被命名的model属性中,暴露给web视图。支持在 * 在Controller类中注有@RequestMapping的方法使用(这里有点拗口,不过结合下面的使用介绍 * 你就会明白的) */ @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ModelAttribute {
@AliasFor("name") String value() default "";
@AliasFor("value") String name() default "";
booleanbinding() defaulttrue;
}
实际上这个注解的作用就是,允许你往 Model 中注入全局属性(可以供所有Controller中注有@Request Mapping的方法使用),value 和 name 用于指定属性的 key ,binding 表示是否绑定,默认为 true。
具体使用方法如下:
全局参数绑定
方式一:
1 2 3 4 5 6 7 8
@ControllerAdvice publicclassMyGlobalHandler { @ModelAttribute publicvoidpresetParam(Model model){ model.addAttribute("globalAttr","this is a global attribute"); } }
这种方式比较灵活,需要什么自己加就行了,加多少属性自己控制
方式二:
1 2 3 4 5 6 7 8 9 10 11 12 13
@ControllerAdvice public classMyGlobalHandler {
@ModelAttribute() public Map<String, String> presetParam(){ Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); returnmap; }
/** * Annotation that identifies methods which initialize the * {@link org.springframework.web.bind.WebDataBinder} which * will be used for populating command and form object arguments * of annotated handler methods. * 粗略翻译:此注解用于标记那些 (初始化[用于组装命令和表单对象参数的]WebDataBinder)的方法。 * 原谅我的英语水平,翻译起来太拗口了,从句太多就用‘()、[]’分割一下便于阅读 * * Init-binder methods must not have a return value; they are usually * declared as {@code void}. * 粗略翻译:初始化绑定的方法禁止有返回值,他们通常声明为 'void' * * <p>Typical arguments are {@link org.springframework.web.bind.WebDataBinder} * in combination with {@link org.springframework.web.context.request.WebRequest} * or {@link java.util.Locale}, allowing to register context-specific editors. * 粗略翻译:典型的参数是`WebDataBinder`,结合`WebRequest`或`Locale`使用,允许注册特定于上下文的编辑器。 * * 总结如下: * 1. @InitBinder 标识的方法的参数通常是 WebDataBinder。 * 2. @InitBinder 标识的方法,可以对 WebDataBinder 进行初始化。WebDataBinder 是 DataBinder 的一个子类,用于完成由表单字段到 JavaBean 属性的绑定。 * 3. @InitBinder 标识的方法不能有返回值,必须声明为void。 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface InitBinder { /** * The names of command/form attributes and/or request parameters * that this init-binder method is supposed to apply to. * <p>Default is to apply to all command/form attributes and all request parameters * processed by the annotated handler class. Specifying model attribute names or * request parameter names here restricts the init-binder method to those specific * attributes/parameters, with different init-binder methods typically applying to * different groups of attributes or parameters. * 粗略翻译:此init-binder方法应该应用于的命令/表单属性和/或请求参数的名称。默认是应用于所有命 * 令/表单属性和所有由带注释的处理类处理的请求参数。这里指定模型属性名或请求参数名将init-binder * 方法限制为那些特定的属性/参数,不同的init-binder方法通常应用于不同的属性或参数组。 * 我至己都理解不太理解这说的是啥呀,我们还是看例子吧 */ String[]value() default {}; }