Spring Webflow和类型转换以进行日期格式化 [英] Spring webflow and type conversion for date formatting

查看:104
本文介绍了Spring Webflow和类型转换以进行日期格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:

春季3.1.3发行

春季webflow 2.3.0。发行

Spring webflow 2.3.0.RELEASE

我最近在spring source论坛上发布了这个问题。请参阅:
Spring Webflow格式问题

I have posted this question recently on spring source forum. See: Spring webflow formatting issue

我也遵循参考文档中概述的步骤以及stackoverflow中的最佳答案:

I followed the steps outlined in the reference documentation and the top answer in stackoverflow too:

答案

我希望在提交表单并进行绑定时,输入无效的日期值时会引发解析异常。但我看不到这种情况。

I expect when my form submits and binding occurs, that a parse exception is thrown when entering in an invalid date value. But i do not see this happening.

如果并且当引发异常时,我该如何处理它以在前端显示错误消息?

Also if and when the exception is thrown, how do i handle it to display error message on the front end?

我想开始在应用程序中使用类型转换。我按照以下步骤为Spring MVC和SWF配置类型转换格式。

I wanted to start using type conversions in my application. I followed the steps below for configuring type conversion formatting for spring MVC and SWF.

我按照以下步骤添加到servlet上下文中

I followed the steps and added into the servlet context

<!-- Enables controllers mapped with @RequestMapping annotations, formatting annotations @NumberFormat
    @DateTimeFormat, and JSR 303 style validation --> 
   <mvc:annotation-driven conversion-service="applicationConversionService1" />

<webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService" view-factory-creator="mvcViewFactoryCreator" development="true"/>

<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService"> 
        <constructor-arg ref="applicationConversionService1"/> 
   </bean>

我创建了一个新类来注册自己的格式化程序,以在Spring MVC和Spring Web Flow中使用。

I created a new class to register my own formatter for use in both Spring MVC and in Spring Web Flow.

@Component(value = "applicationConversionService1")
public class ApplicationConversionServiceFactoryBean extends
        FormattingConversionServiceFactoryBean {

    @Override
    protected void installFormatters(FormatterRegistry registry) {
      // Register the default date formatter provided by Spring
      registry.addFormatter(new DateFormatter("dd/MM/yyyy"));
    } 

}

现在,当我部署到服务器时,上下文已正确初始化,并且类路径扫描注册了bean。

Now when i deploy to the server, the context is initialised correctly and classpath scanning registers the bean.

在绑定的表单模型中,我用新的dateformat批注注释了date字段。

In my form model that binds, i have annotated a date field with the new dateformat annotation.

@DateTimeFormat(pattern="dd/MM/yyyy")
    private Date revisedTermExpiryDate = new Date();

更新:
我正在调试,因为我在类路径上有joda-time spring正在注册joda DateTimeFormatter,并且解析没有失败。

UPDATE: I was debugging and as i have joda-time on my classpath i believe spring is registering joda DateTimeFormatter instead and the parsing is not failing.

如果我实际上删除了注释,它将调用DateFormatter,该异常将按预期引发解析异常,但该异常会在框架中被吞没。

If i remove the annotation actually it calls DateFormatter which throws parse exception as expected but the exception is swallowed in the framework.

我的目标是用全局默认日期注册普通的DateFormatter,严格解析(lenient = false)并优雅地处理所有解析异常。对我来说,这是一个很棒的功能,而不是在每个Web流的验证器中处理解析日期。

My objective is to register the plain DateFormatter with a global default date, parse strictly (lenient=false) and handle any parse exceptions gracefully. This to me is a great feature to have rather than handling parsing dates in validator for every web flow.

谢谢,
Shane。

Thanks, Shane.

推荐答案

更新2:

我发现了为什么在我们的应用程序中吞噬了异常,我们在验证时从消息上下文中清除了消息。

I found why the exception was swallowed in our application, We were clearing the messages from the message context when validating.

因此,如果日期格式化程序由于无效值而引发异常由用户输入,spring可以很好地处理此问题,并在与该字段相关的消息上下文中创建四个错误消息键。例如,如果字段modifiedTermExpiryDate失败,则会创建四个新键:

So If the date formatter throws an exception due to invalid value inputted by the user, spring handles this gracefully and creates four error message keys in the message context related to that field. For example if field revisedTermExpiryDate fails, four new keys will be created:


  1. recordDecisionFormModel.revisedTermExpiryDate.type Mismatch

  2. revisedTermExpiryDate.typeMismatch

  3. java.util.Date.typeMismatch

  4. typeMismatch

  1. recordDecisionFormModel.revisedTermExpiryDate.type Mismatch
  2. revisedTermExpiryDate.typeMismatch
  3. java.util.Date.typeMismatch
  4. typeMismatch

所有您需要做的就是在您的messages.properties文件中指定其中一项与验证消息。

All you need to do is specify in your messages.properties one of these keys with your validation message.

示例:

revisedTermExpiryDate.typeMismatch=Revised Term Expiry Date: DATE IS INVALID

已解决!

这篇关于Spring Webflow和类型转换以进行日期格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆