能够通过提供自定义方法处理器来做到这一点。下面是示例代码。
自定义实现:
public class OrderControllerArgumentResolver extends
ModelAttributeMethodProcessor {
public OrderControllerArgumentResolver() {
super(true);
}
@Override
public boolean supportsParameter(MethodParameter paramMethodParameter) {
//Resolve - custom annotation created to differentiate order parameter from others.
//This is to make sure this resolver is used only while resolving OrderDTO is used as method parameter
if (paramMethodParameter.getParameterAnnotation(Resolve.class) != null) {
return true;
}
return false;
}
@Override
protected final Object createAttribute(String attributeName,
MethodParameter parameter, WebDataBinderFactory binderFactory,
NativeWebRequest request) throws Exception {
//Instantiate appropriate order instance based on input.
return BeanUtils.instantiateClass(OrderTypeEnum.getDTOClass(request
.getParameter("orderType")));
}
@Override
protected void bindRequestParameters(WebDataBinder binder,
NativeWebRequest request) {
ServletRequest servletRequest = request
.getNativeRequest(ServletRequest.class);
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
servletBinder.bind(servletRequest);
}
}
配置:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean
class="test.util.OrderControllerArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
能够通过这种方法达到预期的结果。但不确定这种方法是否会导致任何其他问题。请建议。