绑定Spring MVC命令对象时如何自定义参数名称?

绑定Spring MVC命令对象时如何自定义参数名称?,第1张

绑定Spring MVC命令对象时如何自定义参数名称?

这是我的工作:

首先,一个参数解析器:

public class AnnotationServletModelAttributeResolver extends ServletModelAttributeMethodProcessor {        private ConcurrentMap<Class<?>, Map<String, String>> definitionsCache = Maps.newConcurrentMap();    public AnnotationServletModelAttributeResolver(boolean annotationNotRequired) {        super(annotationNotRequired);    }    @Override    public boolean supportsParameter(MethodParameter parameter) {        if (parameter.getParameterType().isAnnotationPresent(SupportsAnnotationParameterResolution.class)) { return true;        }        return false;    }    @Override    protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {        ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);        ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;        bind(servletRequest, servletBinder);    }    @SuppressWarnings("unchecked")    public void bind(ServletRequest request, ServletRequestDataBinder binder) {        Map<String, ?> propertyValues = parsePropertyValues(request, binder);        MutablePropertyValues mpvs = new MutablePropertyValues(propertyValues);        MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);        if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs);        }        // two lines copied from ExtendedServletRequestDataBinder        String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;        mpvs.addPropertyValues((Map<String, String>) request.getAttribute(attr));        binder.bind(mpvs);    }    private Map<String, ?> parsePropertyValues(ServletRequest request, ServletRequestDataBinder binder) {        // similar to WebUtils.getParametersStartingWith(..) (prefixes not supported)        Map<String, Object> params = Maps.newTreeMap();        Assert.notNull(request, "Request must not be null");        Enumeration<?> paramNames = request.getParameterNames();        Map<String, String> parameterMappings = getParameterMappings(binder);        while (paramNames != null && paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); String[] values = request.getParameterValues(paramName); String fieldName = parameterMappings.get(paramName); // no annotation exists, use the default - the param name=field name if (fieldName == null) {     fieldName = paramName; } if (values == null || values.length == 0) {     // Do nothing, no values found at all. } else if (values.length > 1) {     params.put(fieldName, values); } else {     params.put(fieldName, values[0]); }        }        return params;    }        private Map<String, String> getParameterMappings(ServletRequestDataBinder binder) {        Class<?> targetClass = binder.getTarget().getClass();        Map<String, String> map = definitionsCache.get(targetClass);        if (map == null) { Field[] fields = targetClass.getDeclaredFields(); map = Maps.newHashMapWithExpectedSize(fields.length); for (Field field : fields) {     CommandParameter annotation = field.getAnnotation(CommandParameter.class);     if (annotation != null && !annotation.value().isEmpty()) {         map.put(annotation.value(), field.getName());     } } definitionsCache.putIfAbsent(targetClass, map); return map;        } else { return map;        }    }        protected void bindMultipart(Map<String, List<MultipartFile>> multipartFiles, MutablePropertyValues mpvs) {        for (Map.Entry<String, List<MultipartFile>> entry : multipartFiles.entrySet()) { String key = entry.getKey(); List<MultipartFile> values = entry.getValue(); if (values.size() == 1) {     MultipartFile value = values.get(0);     if (!value.isEmpty()) {         mpvs.add(key, value);     } } else {     mpvs.add(key, values); }        }    }}

然后使用后处理器注册参数解析器。它应该注册为

<bean>

public class AnnotationHandlerMappingPostProcessor implements BeanPostProcessor {    @Override    public Object postProcessAfterInitialization(Object bean, String arg1) throws BeansException {        return bean;    }    @Override    public Object postProcessBeforeInitialization(Object bean, String arg1) throws BeansException {        if (bean instanceof RequestMappingHandlerAdapter) { RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean; List<HandlerMethodArgumentResolver> resolvers = adapter.getCustomArgumentResolvers(); if (resolvers == null) {     resolvers = Lists.newArrayList(); } resolvers.add(new AnnotationServletModelAttributeResolver(false)); adapter.setCustomArgumentResolvers(resolvers);        }        return bean;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4908416.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存