Search in sources :

Example 21 with ModelAttribute

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);
            }
        }
    }
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute)

Example 22 with ModelAttribute

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);
    }
}
Also used : ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method)

Example 23 with ModelAttribute

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));
}
Also used : ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute)

Example 24 with ModelAttribute

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;
}
Also used : OfficeLevelDto(org.mifos.dto.domain.OfficeLevelDto) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with ModelAttribute

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;
}
Also used : ProductCategoryDetailsDto(org.mifos.dto.screen.ProductCategoryDetailsDto) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ModelAttribute (org.springframework.web.bind.annotation.ModelAttribute)35 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Locale (java.util.Locale)3 DBObject (com.mongodb.DBObject)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Method (java.lang.reflect.Method)2 BigDecimal (java.math.BigDecimal)2 List (java.util.List)2 Valid (javax.validation.Valid)2 MongoConstants (org.devgateway.ocds.persistence.mongo.constants.MongoConstants)2 YearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest)2 LoanProductRequest (org.mifos.dto.domain.LoanProductRequest)2 ReportCategoryDto (org.mifos.dto.domain.ReportCategoryDto)2 LoanProductFormDto (org.mifos.dto.screen.LoanProductFormDto)2 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BasicDBObject (com.mongodb.BasicDBObject)1