use of org.springframework.web.bind.annotation.ModelAttribute in project spring-framework by spring-projects.
the class ModelFactory method invokeModelAttributeMethods.
/**
* Invoke model attribute methods to populate the model.
* Attributes are added only if not already present in the model.
*/
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container) throws Exception {
while (!this.modelMethods.isEmpty()) {
InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod();
ModelAttribute ann = modelMethod.getMethodAnnotation(ModelAttribute.class);
if (container.containsAttribute(ann.name())) {
if (!ann.binding()) {
container.setBindingDisabled(ann.name());
}
continue;
}
Object returnValue = modelMethod.invokeForRequest(request, container);
if (!modelMethod.isVoid()) {
String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
if (!ann.binding()) {
container.setBindingDisabled(returnValueName);
}
if (!container.containsAttribute(returnValueName)) {
container.addAttribute(returnValueName, returnValue);
}
}
}
}
use of org.springframework.web.bind.annotation.ModelAttribute in project spring-framework by spring-projects.
the class ModelFactory method getNameForReturnValue.
/**
* 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);
}
}
use of org.springframework.web.bind.annotation.ModelAttribute in project spring-framework by spring-projects.
the class ModelFactory method getNameForParameter.
/**
* 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));
}
use of org.springframework.web.bind.annotation.ModelAttribute in project head by mifos.
the class ViewOfficeHierarchyController method showPopulatedCheckboxForm.
@RequestMapping(method = RequestMethod.GET)
@ModelAttribute("formBean")
public ViewOfficeHierarchyFormBean showPopulatedCheckboxForm() {
OfficeLevelDto officeLevels = adminServiceFacade.retrieveOfficeLevelsWithConfiguration();
ViewOfficeHierarchyFormBean formBean = new ViewOfficeHierarchyFormBean();
formBean.setHeadOffice(officeLevels.isHeadOfficeEnabled());
formBean.setRegionalOffice(officeLevels.isRegionalOfficeEnabled());
formBean.setSubRegionalOffice(officeLevels.isSubRegionalOfficeEnabled());
formBean.setAreaOffice(officeLevels.isAreaOfficeEnabled());
formBean.setBranchOffice(officeLevels.isBranchOfficeEnabled());
return formBean;
}
use of org.springframework.web.bind.annotation.ModelAttribute in project head by mifos.
the class EditProductCategoryController method showCategory.
@RequestMapping(method = RequestMethod.GET)
@ModelAttribute("formBean")
public ProductCategoryFormBean showCategory(@RequestParam(value = "globalPrdCategoryNum", required = true) String globalPrdCategoryNum) {
ProductCategoryDetailsDto productCategoryDetailsDto = adminServiceFacade.retrieveProductCateogry(globalPrdCategoryNum);
ProductCategoryFormBean productCategoryFormBean = new ProductCategoryFormBean();
productCategoryFormBean.setGlobalPrdCategoryNum(globalPrdCategoryNum);
productCategoryFormBean.setProductCategoryDesc(productCategoryDetailsDto.getProductCategoryDesc());
productCategoryFormBean.setProductCategoryName(productCategoryDetailsDto.getProductCategoryName());
productCategoryFormBean.setProductCategoryStatusId(productCategoryDetailsDto.getProductCategoryStatusId().toString());
productCategoryFormBean.setProductType(productCategoryDetailsDto.getProductTypeName());
productCategoryFormBean.setProductTypeId(productCategoryDetailsDto.getProductTypeId().toString());
return productCategoryFormBean;
}
Aggregations