1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.controller;
17
18 import org.kuali.rice.krad.uif.UifConstants;
19 import org.kuali.rice.krad.web.form.UifFormBase;
20 import org.springframework.core.MethodParameter;
21 import org.springframework.validation.BindException;
22 import org.springframework.validation.Errors;
23 import org.springframework.web.bind.ServletRequestDataBinder;
24 import org.springframework.web.bind.WebDataBinder;
25 import org.springframework.web.bind.annotation.ModelAttribute;
26 import org.springframework.web.bind.support.WebDataBinderFactory;
27 import org.springframework.web.context.request.NativeWebRequest;
28 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
29 import org.springframework.web.method.support.ModelAndViewContainer;
30
31 import javax.servlet.ServletRequest;
32 import java.util.Map;
33
34
35
36
37 public class UifDefaultFormMethodArgumentResolver implements HandlerMethodArgumentResolver {
38
39
40
41
42 public UifDefaultFormMethodArgumentResolver() {
43 super();
44 }
45
46
47
48
49 @Override
50 public boolean supportsParameter(MethodParameter parameter) {
51 boolean isUifForm = false;
52
53 if (!parameter.hasParameterAnnotation(ModelAttribute.class) && UifFormBase.class.isAssignableFrom(
54 parameter.getParameterType())) {
55 isUifForm = true;
56 }
57
58 return isUifForm;
59 }
60
61
62
63
64 @Override
65 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
66 NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
67 String name = UifConstants.DEFAULT_MODEL_NAME;
68
69 Object attribute = null;
70 if (mavContainer.containsAttribute(name)) {
71 attribute = mavContainer.getModel().get(name);
72 }
73
74 WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);
75 if (binder.getTarget() != null) {
76 ServletRequest servletRequest = webRequest.getNativeRequest(ServletRequest.class);
77 ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
78 servletBinder.bind(servletRequest);
79
80 if (binder.getBindingResult().hasErrors()) {
81 if (isBindExceptionRequired(parameter)) {
82 throw new BindException(binder.getBindingResult());
83 }
84 }
85 }
86
87 Map<String, Object> bindingResultModel = binder.getBindingResult().getModel();
88 mavContainer.removeAttributes(bindingResultModel);
89 mavContainer.addAllAttributes(bindingResultModel);
90
91 return binder.getTarget();
92 }
93
94
95
96
97
98
99
100 protected boolean isBindExceptionRequired(MethodParameter parameter) {
101 int i = parameter.getParameterIndex();
102 Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
103 boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
104
105 return !hasBindingResult;
106 }
107
108 }