我目前正在 Dispatcher-Servlet.xml 中使用 MappingJacksonHttpMessageConverter。在我的应用程序中,当用户输入货币时,逗号本身会附加到该值上。当该值传递到 Controller 时,无法将带逗号的字符串转换为 double 值。引发配对错误。我将此值用于其他计算,因此我无法在模态类中将其类型更改为字符串。
有没有办法可以使用MappingJacksonHttpMessageConverter让 Controller 忽略逗号并读取值。
这是我的 servlet.xml 文件的一部分。
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="order" value="1" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
如有任何帮助,我们将不胜感激
最佳答案
您可以通过实现 Converter 接口(interface)在 Spring 中创建自定义转换服务来实现此目的。 有关这方面的信息,请参阅 http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html 和 http://log4aj.blogspot.in/2012/08/spring-converterconversionservice.html .
关于java - 在 Spring MVC 中将带逗号的字符串转换为 double 字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24197141/