在基于 Spring 3 的 web (portlet) 应用程序中,我有一个 Controller ,其方法如下:
@RenderMapping
public ModelAndView handleRenderRequest(...,@RequestParam MyClass myObject)
{
...
}
现在我想知道:我如何告诉spring如何将请求参数转换为MyClass。我找到了关于属性编辑器和 Converter 接口(interface)的信息,并且似乎暗示 Converter 是属性编辑器的继承者,但似乎没有人喜欢明确说明它。
我实现了 String 到 MyClass 转换的转换器接口(interface)。但是我该如何告诉 Spring 呢?我尽可能使用基于注释的配置,所以我检查了 spring 是否会自动从我的类路径中检测到 Converter,但它没有。
所以认为 Configuring a ConversionService 的部分从手册想告诉我,我必须将以下内容添加到我的 applicationContext.xml 中:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="some.package.MyConverter"/>
</list>
</property>
</bean>
有点静止:
org.springframework.beans.ConversionNotSupportedException: Failed to convert value [...]
那么我错过了什么?有没有办法,只配置一个包,让 spring 扫描这个包中的转换器并自动注册它们?并说在某种方法中我想使用与所有其他方法不同的转换器。例如,我想检查一个具有 Luhn-Checksum 的整数并删除校验和,我该怎么做? @RequestParam(converter=some.package.MyConverter.class) 之类的东西会很棒。
编辑
好的,我刚刚在文档中发现:
Use the Formatter SPI when you're working in a client environment, such as a web application, and need to parse and print localized field values
所以我想这意味着我应该使用 Formatter SPI,这是属性编辑器和转换器旁边的第三种可能性(我想我真的可以使用比较表或类似的东西)。我也实现了 Parser 接口(interface),并尝试使用以下方法注册我的转换器:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.SortOrderEnumConverterSpring"/>
</set>
</property>
</bean>
如您所见,我使用“set”而不是“list”来指定转换器。我在 FormattingConversionServiceFactoryBean.setConverters 方法中设置了一个调试断点,该方法在使用 list 时不会触发,但在使用 set 时会触发。
另外我加了
<mvc:annotation-driven conversion-service="conversionService"/>
以及我的 applicationContext 的 mvc 前缀的命名空间。但我仍然得到不支持转换的异常。
我还尝试回到转换器方法,并在我的 applicationContext.xml 文件中将转换器的参数列表从列表更改为设置,但这也没有改变任何内容。
编辑2
正如 digitaljoel 指出的,可以使用 initBinder 方法为每个 Controller 设置不同的转换器。我将此应用于我的 Controller :
@Autowired
private ConversionService conversionService;
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.setConversionService(conversionService);
}
在我的 applicationContext.xml 中:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.with.MyConverter"/>
</set>
</property>
</bean>
突然之间,转换工作正常:-)。但是我对必须将其应用于我的每个 Controller 并不十分满意。必须有一种方法可以在我的 applicationContext 中为每个人设置它,不是吗?很高兴知道我可以在需要时覆盖默认值(毕竟我要求这样做),但我仍然想设置默认值。
那么格式化程序的东西呢。我不应该使用它而不是 Converter 吗?
最佳答案
Spring Portlet MVC 3.0 不支持
<mvc:annotation-driven conversion-service="conversionService"/>
访问 https://jira.springsource.org/browse/SPR-6817了解更多信息。
但是您可以将其添加到您的通用应用程序上下文中
<bean
class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService">
<list>
<ref bean="conversionService" />
</list>
</property>
</bean>
</property>
</bean>
这样你就不需要为每个 Controller 添加@InitBinder
当然
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<!-- converter implementations here -->
</list>
</property>
</bean>
关于spring - spring请求参数转换怎么做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6835615/