@modelAtrribute注解学习
基本介绍:springWeb中的注解;作用在参数和方法上。简单来说就是讲请求端表单参数与实体类进行一一对应,名字必须相同。
下面是源码:
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ModelAttribute {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean binding() default true;
}
注意:@ModelAtrribute 放在方法上,它会执行在controller中未添加该注解的方法之前。
注解参数:
参数value和name它们都用@AliasFor修饰(互为别名)也就是说,它们的作用是一样的。
声明绑定模型名称。
下面为源码解释:
/**
* The name of the model attribute to bind to.
* <p>The default model attribute name is inferred from the declared
* attribute type (i.e. the method parameter type or method return type),
* based on the non-qualified class name:
* e.g. "orderAddress" for class "mypackage.OrderAddress",
* or "orderAddressList" for "List<mypackage.OrderAddress>".
* @since 4.3
*/
要绑定到的模型属性的名称。
默认的模型属性名是从声明中推断出来的
属性类型(即方法参数类型或方法返回类型),
基于非限定类名:
如。"orderAddress"为类"mypackage。OrderAddress”,
或“orderAddressList”为“List<mypackage.OrderAddress>”。
参数binding:是否允许绑定模型,默认为true
/**
* Allows declaring data binding disabled directly on an {@code @ModelAttribute}
* method parameter or on the attribute returned from an {@code @ModelAttribute}
* method, both of which would prevent data binding for that attribute.
* <p>By default this is set to {@code true} in which case data binding applies.
* Set this to {@code false} to disable data binding.
* @since 4.3
*/
361/5000
允许在{@code @ModelAttribute}上直接声明禁用数据绑定
方法参数或从{@code @ModelAttribute}返回的属性方法,
两者都将防止该属性的数据绑定。
默认情况下,它被设置为{@code true},在这种情况下,数据绑定应用。
设置为{@code false}以禁用数据绑定。