我正在尝试将 Spring MVC 应用程序从 3.0.6 升级到 3.1.2,而一些曾经工作的控制器似乎不再工作了。我已经阅读了 spring 文档,但我对什么与什么兼容感到困惑。
我们有一个 CustomWebArgumentResolver,它查找任何名为“asOf”的请求参数并将其值转换为日期。我们称其为“AsOfDateConverter”。升级到 spring-3.1.2 时,我利用了新的命名空间功能并将其添加到我的 applicationContext 中:
<mvc:annotation-driven conversion-service="conversionService">
<mvc:argument-resolvers>
<bean id="customWebArgumentResolver" class="my.converters.CustomWebArgumentResolver">
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
CustomWebArgumentResolver 很简单:
public class CustomWebArgumentResolver implements WebArgumentResolver {
private AsOfDateConverter asOfDateConverter;
@Override
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
if (isAsOfDateParameter(methodParameter)) {
return asOfDateConverter.convert(webRequest.getParameter("asOf"));
}
return UNRESOLVED;
}
然后一个示例控制器可能看起来像这样:
@Controller
@Secured({BaseController.ROLE_LOGGED_IN})
@org.springframework.transaction.annotation.Transactional
public class DashboardController extends BaseController {
public static final String URL = "/dashboard";
@RequestMapping(value=URL, method=RequestMethod.GET)
public ModelAndView get(@RequestParam(required=false) String requestedMeterType, @AsOf Date asOf) {
debug(log, "Rendering dashboard asOf %s", asOf);
etc etc
“asOf”参数为空,我确定我遗漏了一些明显的东西。如果有人深入了解最新的 MVC 3.1 内容,可以为我指明正确的方向,我将不胜感激。
谢谢!汤姆
编辑: AsOf 注释:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface AsOf {
}
更多我的应用程序上下文:
<mvc:annotation-driven conversion-service="conversionService">
<mvc:argument-resolvers>
<bean class="[blah].AsOfDateHandlerMethodArgumentResolver">
<property name="asOfDateConverter">
<bean class="[blah].AsOfDateConverter"/>
</property>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
<!-- Added to re-support @Controller annotation scanning after upgrading to spring-3.1. -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="[blah].converters.CustomerConverter"/>
<bean class="[blah].converters.AccountConverter"/>
<bean class="[blah].converters.DateConverter"/>
<bean class="[blah].converters.CustomerCommunicationInstanceConverter"/>
<bean class="[blah].converters.MeterTypeConverter"/>
<bean class="[blah].converters.AreaAmountConverter" p:precision="0"/>
<bean class="[blah].converters.LengthAmountConverter" p:precision="1"/>
</set>
</property>
</bean>