【modelAtrribute】

@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&lt;mypackage.OrderAddress&gt;".
	 * @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}以禁用数据绑定。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值