spring mvc @ModelAttribute 基本类型 自定义对象解析流程
生活随笔
收集整理的這篇文章主要介紹了
spring mvc @ModelAttribute 基本类型 自定义对象解析流程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、如果方法中參數為基本類型,則通過POST的表單或者GET參數會使用
RequestParamMethodArgumentResolver作為參數解析器進行轉換。如果參數名和客戶端傳入的參數名不一樣,則加入@RequestParam注解。 @RequestMapping("/user")@ResponseBodypublic Map<String,Object> getUser(Integer id) throws Exception{log.info(" get user id:" + id);Map<String,Object> user = new HashMap<String,Object>();user.put("id",id );user.put("name","liming" );return user; // return "liming";}?二、如果方法中參數為自定義對象,則通過POST的表單或者GET參數 不論是否加上@ModelAttribute,都會自動轉換為對象。此時的resolver為
ServletModelAttributeMethodProcessor ModelAttributeMethodProcessor public boolean supportsParameter(MethodParameter parameter) {return (parameter.hasParameterAnnotation(ModelAttribute.class) ||(this.annotationNotRequired && !BeanUtils.isSimpleProperty(parameter.getParameterType())));}?ServletModelAttributeMethodProcessor.resolveArgument參數會先創建
WebDataBinder的實現類為ExtendedServletRequestDataBinder, public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {Object attribute = null;BindingResult bindingResult = null;if (bindingResult == null) {// Bean property binding and validation;// skipped in case of binding failure on construction.WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);if (binder.getTarget() != null) {if (!mavContainer.isBindingDisabled(name)) {bindRequestParameters(binder, webRequest);}validateIfApplicable(binder, parameter);if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {throw new BindException(binder.getBindingResult());}}// Value type adaptation, also covering java.util.Optionalif (!parameter.getParameterType().isInstance(attribute)) {attribute = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType(), parameter);}bindingResult = binder.getBindingResult();}return attribute;}然后會調用ServletModelAttributeMethodProcessor.bindRequestParameters將request中的表單或者GET參數數據,綁定到binder中。
ServletModelAttributeMethodProcessor protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);Assert.state(servletRequest != null, "No ServletRequest");ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;servletBinder.bind(servletRequest);}最終調用到ServletRequestDataBinder.binder首先將入參轉換為MutablePropertyValues,然后映射到WebBinder中。
ServletRequestDataBinderpublic void bind(ServletRequest request) {MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);if (multipartRequest != null) {bindMultipart(multipartRequest.getMultiFileMap(), mpvs);}addBindValues(mpvs, request);doBind(mpvs);}?
總結
以上是生活随笔為你收集整理的spring mvc @ModelAttribute 基本类型 自定义对象解析流程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring mvc @RequestB
- 下一篇: springboot @Configur