List of usage examples for org.springframework.web.method.support ModelAndViewContainer addAttribute
public ModelAndViewContainer addAttribute(String name, @Nullable Object value)
From source file:com.ufukuzun.myth.dialect.handler.AjaxRequestResponseBodyReturnValueHandler.java
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { AjaxRequestBody ajaxRequestBody = parameter.getParameterAnnotation(AjaxRequestBody.class); boolean performValidation = ajaxRequestBody.validate(); String targetName = ajaxRequestBody.targetName(); Assert.notNull(targetName);// ww w . j a va2s . co m Object argument = readWithMessageConverters(webRequest, parameter, parameter.getGenericParameterType()); if (argument != null && AjaxRequest.class.isInstance(argument) && !StringUtils.isEmpty(targetName)) { AjaxRequest<?> ajaxRequest = (AjaxRequest<?>) argument; mavContainer.addAttribute(targetName, ajaxRequest.getModel()); if (performValidation) { ajaxRequest .setModelValid(myth.validate(mavContainer.getModel(), ajaxRequest.getModel(), targetName)); } } return argument; }
From source file:de.iew.web.isc.spring.IscRequestMethodArgumentResolver.java
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception { DSRequest dsRequest = new DSRequest(); WebDataBinder binder = webDataBinderFactory.createBinder(nativeWebRequest, dsRequest, "dsRequest"); binder.setFieldMarkerPrefix("__"); // Man kann noch eigene Property Editoren konfigurieren //binder.registerCustomEditor(); if (binder instanceof ServletRequestDataBinder) { /*//from ww w . j av a 2s.co m @TODO Wir machen es uns etwas leicht. Unser DSRequest Objekt muss eigentlich nur die isc_-Eigenschaften kennen. Dort ist auch isc_metaDataPreifx konfiguriert. Anhand dieses Prefix mssen wir die anderen Eiegenschaften auflsen. */ ServletRequestDataBinder servletRequestDataBinder = (ServletRequestDataBinder) binder; HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); servletRequestDataBinder.bind(request); BindingResult bindingResult = binder.getBindingResult(); modelAndViewContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + "dsRequest", bindingResult); return dsRequest; } else { throw new UnsupportedOperationException( "Using " + binder.getClass() + " WebDataBinder is not supported."); } }
From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java
/** * Resolve the argument from the model or if not found instantiate it with its default if it is available. The model * attribute is then populated with request values via data binding and optionally validated if * {@code @java.validation.Valid} is present on the argument. * //from ww w . ja va 2 s . co m * @throws org.springframework.validation.BindException if data binding and validation result in an error and the * next method parameter is not of type {@link org.springframework.validation.Errors}. * @throws Exception if WebDataBinder initialization fails. */ public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception { String name = parameter.getParameterAnnotation(FormBean.class).value(); Object target = (mavContainer.containsAttribute(name)) ? mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request); WebDataBinder binder = binderFactory.createBinder(request, target, name); target = binder.getTarget(); if (target != null) { bindRequestParameters(mavContainer, binderFactory, binder, request, parameter); validateIfApplicable(binder, parameter); if (binder.getBindingResult().hasErrors()) { if (isBindExceptionRequired(binder, parameter)) { throw new BindException(binder.getBindingResult()); } } } target = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType()); mavContainer.addAttribute(name, target); return target; }
From source file:org.zht.framework.web.bind.resolver.FormModelMethodArgumentResolver.java
/** * Resolve the argument from the model or if not found instantiate it with * its default if it is available. The model attribute is then populated * with request values via data binding and optionally validated * if {@code @java.validation.Valid} is present on the argument. * * @throws org.springframework.validation.BindException * if data binding and validation result in an error * and the next method parameter is not of type {@link org.springframework.validation.Errors}. * @throws Exception if WebDataBinder initialization fails. *///from w w w .ja v a 2 s. c o m public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception { String name = parameter.getParameterAnnotation(FormModel.class).value(); Object target = (mavContainer.containsAttribute(name)) ? mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request); WebDataBinder binder = binderFactory.createBinder(request, target, name); target = binder.getTarget(); if (target != null) { bindRequestParameters(mavContainer, binderFactory, binder, request, parameter); validateIfApplicable(binder, parameter); if (binder.getBindingResult().hasErrors()) { List<BindingResult> list = (List<BindingResult>) request .getAttribute(ValidateConstant.BINDING_RESULT_LIST_NAME, Integer.MAX_VALUE / 2 + 1); if (null == list) { list = new ArrayList<BindingResult>(); request.setAttribute(ValidateConstant.BINDING_RESULT_LIST_NAME, list, 0); } list.add(binder.getBindingResult()); request.setAttribute(ValidateConstant.BINDING_RESULT_HAS_ERROR, true, 0); // if (isBindExceptionRequired(binder, parameter)) { // throw new BindException(binder.getBindingResult()); // } } } target = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType()); mavContainer.addAttribute(name, target); return target; }
From source file:org.springframework.web.method.annotation.ModelAttributeMethodProcessor.java
/** * Add non-null return values to the {@link ModelAndViewContainer}. *///from w w w. j av a 2 s . c o m @Override public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnValue != null) { String name = ModelFactory.getNameForReturnValue(returnValue, returnType); mavContainer.addAttribute(name, returnValue); } }
From source file:org.springframework.web.method.annotation.ModelFactory.java
/** * Populate the model in the following order: * <ol>//from w ww. jav a 2 s.co m * <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}. * <li>Invoke {@code @ModelAttribute} methods * <li>Find {@code @ModelAttribute} method arguments also listed as * {@code @SessionAttributes} and ensure they're present in the model raising * an exception if necessary. * </ol> * @param request the current request * @param container a container with the model to be initialized * @param handlerMethod the method for which the model is initialized * @throws Exception may arise from {@code @ModelAttribute} methods */ public void initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod) throws Exception { Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request); container.mergeAttributes(sessionAttributes); invokeModelAttributeMethods(request, container); for (String name : findSessionAttributeArguments(handlerMethod)) { if (!container.containsAttribute(name)) { Object value = this.sessionAttributesHandler.retrieveAttribute(request, name); if (value == null) { throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name); } container.addAttribute(name, value); } } }
From source file:org.springframework.web.method.annotation.ModelFactory.java
/** * Invoke model attribute methods to populate the model. * Attributes are added only if not already present in the model. */// w w w . ja va2 s. c om private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container) throws Exception { while (!this.modelMethods.isEmpty()) { InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod(); ModelAttribute ann = modelMethod.getMethodAnnotation(ModelAttribute.class); Assert.state(ann != null, "No ModelAttribute annotation"); 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); } } } }
From source file:org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor.java
/** * Add non-null return values to the {@link ModelAndViewContainer}. *///from w w w.j a v a2s . c om public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnValue != null) { String name = ModelFactory.getNameForReturnValue(returnValue, returnType); mavContainer.addAttribute(name, returnValue); } }