001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.bind.annotation; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025import org.springframework.core.annotation.AliasFor; 026import org.springframework.ui.Model; 027 028/** 029 * Annotation that binds a method parameter or method return value 030 * to a named model attribute, exposed to a web view. Supported 031 * for controller classes with {@link RequestMapping @RequestMapping} 032 * methods. 033 * 034 * <p>Can be used to expose command objects to a web view, using 035 * specific attribute names, through annotating corresponding 036 * parameters of an {@link RequestMapping @RequestMapping} method. 037 * 038 * <p>Can also be used to expose reference data to a web view 039 * through annotating accessor methods in a controller class with 040 * {@link RequestMapping @RequestMapping} methods. Such accessor 041 * methods are allowed to have any arguments that 042 * {@link RequestMapping @RequestMapping} methods support, returning 043 * the model attribute value to expose. 044 * 045 * <p>Note however that reference data and all other model content is 046 * not available to web views when request processing results in an 047 * {@code Exception} since the exception could be raised at any time 048 * making the content of the model unreliable. For this reason 049 * {@link ExceptionHandler @ExceptionHandler} methods do not provide 050 * access to a {@link Model} argument. 051 * 052 * @author Juergen Hoeller 053 * @author Rossen Stoyanchev 054 * @since 2.5 055 */ 056@Target({ElementType.PARAMETER, ElementType.METHOD}) 057@Retention(RetentionPolicy.RUNTIME) 058@Documented 059public @interface ModelAttribute { 060 061 /** 062 * Alias for {@link #name}. 063 */ 064 @AliasFor("name") 065 String value() default ""; 066 067 /** 068 * The name of the model attribute to bind to. 069 * <p>The default model attribute name is inferred from the declared 070 * attribute type (i.e. the method parameter type or method return type), 071 * based on the non-qualified class name: 072 * e.g. "orderAddress" for class "mypackage.OrderAddress", 073 * or "orderAddressList" for "List<mypackage.OrderAddress>". 074 * @since 4.3 075 */ 076 @AliasFor("value") 077 String name() default ""; 078 079 /** 080 * Allows declaring data binding disabled directly on an {@code @ModelAttribute} 081 * method parameter or on the attribute returned from an {@code @ModelAttribute} 082 * method, both of which would prevent data binding for that attribute. 083 * <p>By default this is set to {@code true} in which case data binding applies. 084 * Set this to {@code false} to disable data binding. 085 * @since 4.3 086 */ 087 boolean binding() default true; 088 089}