【问题标题】:Convert string with commas to double number in spring MVC在spring MVC中将带逗号的字符串转换为双数
【发布时间】:2014-06-13 03:17:01
【问题描述】:

我目前在我的 Dispatcher-Servlet.xml 中使用 MappingJacksonHttpMessageConverter。在我的应用程序中,当用户输入货币时,逗号本身会附加到值上。当该值传递给控制器​​时,它无法将 带逗号的字符串 转换为 double 值。抛出 pare 错误。我将此值用于其他计算,因此我无法在模态类中将其类型更改为字符串。

有没有办法我们可以使用 MappingJacksonHttpMessageConverter 让控制器忽略逗号并读取值。

这是我的 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>

任何帮助将不胜感激

【问题讨论】:

    标签: java spring spring-mvc servlets spring-data-mongodb


    【解决方案1】:

    您可以通过在 spring 中通过实现 Converter 接口来创建自定义转换服务来做到这一点。 有关这方面的信息,请参阅http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.htmlhttp://log4aj.blogspot.in/2012/08/spring-converterconversionservice.html.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 2012-10-27
      • 1970-01-01
      • 2017-11-07
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多