赞
踩
- @InitBinder
- public void initBinder(WebDataBinder binder) throws Exception {
- binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
- binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
- }
- public final Object resolveArgument(
- MethodParameter parameter, ModelAndViewContainer mavContainer,
- NativeWebRequest request, WebDataBinderFactory binderFactory)
- throws Exception {
-
- String name = ModelFactory.getNameForParameter(parameter);
- Object target = (mavContainer.containsAttribute(name)) ?
- mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);
-
- WebDataBinder binder = binderFactory.createBinder(request, target, name);
- if (binder.getTarget() != null) {
- bindRequestParameters(binder, request);
- validateIfApplicable(binder, parameter);
- if (binder.getBindingResult().hasErrors()) {
- if (isBindExceptionRequired(binder, parameter)) {
- throw new BindException(binder.getBindingResult());
- }
- }
- }
-
- mavContainer.addAllAttributes(binder.getBindingResult().getModel());
- return binder.getTarget();
- }
- public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {
- for (InvocableHandlerMethod binderMebinderMethod thod : this.binderMethods) {
- if (isBinderMethodApplicable(binderMethod, binder)) {
- Object returnValue = binderMethod.invokeForRequest(request, null, binder);
- if (returnValue != null) {
- throw new IllegalStateException("@InitBinder methods should return void: " + binderMethod);
- }
- }
- }
- }
- protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
- ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
- ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
- servletBinder.bind(servletRequest);
- }
- public 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);
- }
- private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws Exception {
- Class<?> handlerType = handlerMethod.getBeanType();
- Set<Method> methods = this.dataBinderFactoryCache.get(handlerType);
- if (methods == null) {
- methods = HandlerMethodSelector.selectMethods(handlerType, INIT_BINDER_METHODS);
- this.dataBinderFactoryCache.put(handlerType, methods);
- }
- List<InvocableHandlerMethod> binderMethods = new ArrayList<InvocableHandlerMethod>();
- for (Method method : methods) {
- InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(handlerMethod.getBean(), method);
- binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);
- binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));
- binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
- binderMethods.add(binderMethod);
- }
- return createDataBinderFactory(binderMethods);
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。