Java Code Examples for org.springframework.web.bind.annotation.ModelAttribute#value()

The following examples show how to use org.springframework.web.bind.annotation.ModelAttribute#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorsMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private Object getErrors(MethodParameter parameter, BindingContext context) {
	Assert.isTrue(parameter.getParameterIndex() > 0,
			"Errors argument must be declared immediately after a model attribute argument");

	int index = parameter.getParameterIndex() - 1;
	MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());

	Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
			"cannot both be declared with an async type wrapper. " +
			"Either declare the @ModelAttribute without an async wrapper type or " +
			"handle a WebExchangeBindException error signal through the async type.");

	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null && StringUtils.hasText(ann.value()) ?
			ann.value() : Conventions.getVariableNameForParameter(attributeParam));
	Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);

	Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +
			"immediately after the @ModelAttribute argument to which it applies. " +
			"For @RequestBody and @RequestPart arguments, please declare them with a reactive " +
			"type wrapper and use its onError operators to handle WebExchangeBindException: " +
			parameter.getMethod());

	return errors;
}
 
Example 2
Source File: ErrorsMethodArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
private Object getErrors(MethodParameter parameter, BindingContext context) {
	Assert.isTrue(parameter.getParameterIndex() > 0,
			"Errors argument must be declared immediately after a model attribute argument");

	int index = parameter.getParameterIndex() - 1;
	MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());

	Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
			"cannot both be declared with an async type wrapper. " +
			"Either declare the @ModelAttribute without an async wrapper type or " +
			"handle a WebExchangeBindException error signal through the async type.");

	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null && StringUtils.hasText(ann.value()) ?
			ann.value() : Conventions.getVariableNameForParameter(attributeParam));
	Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);

	Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +
			"immediately after the @ModelAttribute argument to which it applies. " +
			"For @RequestBody and @RequestPart arguments, please declare them with a reactive " +
			"type wrapper and use its onError operators to handle WebExchangeBindException: " +
			parameter.getMethod());

	return errors;
}
 
Example 3
Source File: ModelFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value. Results will be
 * based on:
 * <ol>
 * <li>the method {@code ModelAttribute} annotation value
 * <li>the declared return type if it is more specific than {@code Object}
 * <li>the actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Assert.state(method != null, "No handler method");
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 4
Source File: ModelFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value. Results will be
 * based on:
 * <ol>
 * <li>the method {@code ModelAttribute} annotation value
 * <li>the declared return type if it is more specific than {@code Object}
 * <li>the actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Assert.state(method != null, "No handler method");
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 5
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value based on:
 * <ol>
 * <li>the method {@code ModelAttribute} annotation value
 * <li>the declared return type if it is more specific than {@code Object}
 * <li>the actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 6
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType,
		Object returnValue, ExtendedModelMap implicitModel) {

	ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
	String attrName = (attr != null ? attr.value() : "");
	if ("".equals(attrName)) {
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
		attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
	}
	implicitModel.addAttribute(attrName, returnValue);
}
 
Example 7
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value using one of:
 * <ol>
 * 	<li>The method {@code ModelAttribute} annotation value
 * 	<li>The declared return type if it is more specific than {@code Object}
 * 	<li>The actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType the return type of the method
 * @return the model name, never {@code null} nor empty
 */
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
	ModelAttribute annotation = returnType.getMethodAnnotation(ModelAttribute.class);
	if (annotation != null && StringUtils.hasText(annotation.value())) {
		return annotation.value();
	}
	else {
		Method method = returnType.getMethod();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, returnType.getContainingClass());
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example 8
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType,
		Object returnValue, ExtendedModelMap implicitModel) {

	ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
	String attrName = (attr != null ? attr.value() : "");
	if ("".equals(attrName)) {
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
		attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
	}
	implicitModel.addAttribute(attrName, returnValue);
}
 
Example 9
Source File: ModelInitializer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Derive the model attribute name for the given method parameter based on
 * a {@code @ModelAttribute} parameter annotation (if present) or falling
 * back on parameter type based conventions.
 * @param parameter a descriptor for the method parameter
 * @return the derived name
 * @see Conventions#getVariableNameForParameter(MethodParameter)
 */
public static String getNameForParameter(MethodParameter parameter) {
	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null ? ann.value() : null);
	return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter));
}
 
Example 10
Source File: ModelFactory.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Derive the model attribute name for the given method parameter based on
 * a {@code @ModelAttribute} parameter annotation (if present) or falling
 * back on parameter type based conventions.
 * @param parameter a descriptor for the method parameter
 * @return the derived name
 * @see Conventions#getVariableNameForParameter(MethodParameter)
 */
public static String getNameForParameter(MethodParameter parameter) {
	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null ? ann.value() : null);
	return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter));
}
 
Example 11
Source File: ModelInitializer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Derive the model attribute name for the given method parameter based on
 * a {@code @ModelAttribute} parameter annotation (if present) or falling
 * back on parameter type based conventions.
 * @param parameter a descriptor for the method parameter
 * @return the derived name
 * @see Conventions#getVariableNameForParameter(MethodParameter)
 */
public static String getNameForParameter(MethodParameter parameter) {
	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null ? ann.value() : null);
	return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter));
}
 
Example 12
Source File: ModelFactory.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Derive the model attribute name for the given method parameter based on
 * a {@code @ModelAttribute} parameter annotation (if present) or falling
 * back on parameter type based conventions.
 * @param parameter a descriptor for the method parameter
 * @return the derived name
 * @see Conventions#getVariableNameForParameter(MethodParameter)
 */
public static String getNameForParameter(MethodParameter parameter) {
	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null ? ann.value() : null);
	return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter));
}
 
Example 13
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Derive the model attribute name for a method parameter based on:
 * <ol>
 * <li>the parameter {@code @ModelAttribute} annotation value
 * <li>the parameter type
 * </ol>
 * @param parameter a descriptor for the method parameter
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForParameter(MethodParameter parameter) {
	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null ? ann.value() : null);
	return (StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter));
}
 
Example 14
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Derives the model attribute name for a method parameter based on:
 * <ol>
 * 	<li>The parameter {@code @ModelAttribute} annotation value
 * 	<li>The parameter type
 * </ol>
 * @return the derived name; never {@code null} or an empty string
 */
public static String getNameForParameter(MethodParameter parameter) {
	ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class);
	String attrName = (annot != null) ? annot.value() : null;
	return StringUtils.hasText(attrName) ? attrName :  Conventions.getVariableNameForParameter(parameter);
}