Struts 1 ActionForms - 表单中的日期约定是什么?

发布于 2024-11-23 19:55:47 字数 429 浏览 3 评论 0 原文

我即将开始使用 Struts 1.2 的 e 项目。没有计划转移到另一个框架。

我想知道处理表单中的日期时的约定是什么?

  1. 我应该创建一个 Date 变量并创建一个将发生转换的 setDate(String date) 方法吗?

  2. 创建一个日期变量,一个setDate(Date date)并在链中的某个位置注册一个特殊的转换器? (不知道是否可能)

  3. 创建一个String变量,一个setDate(String date)并让转换/验证到ActionForm中的validate方法bean?

或者另一种方法?

另外,如果您有任何关于加快使用此框架的建议,我将非常感激。

I am about to start with e project that uses Struts 1.2. There is no plan to move to another framework.

I wanted to know what's the convention when handling a date in the form?

  1. Should I create a Date variable and create a setDate(String date) method where the conversion will occur?

  2. Create a Date variable, a setDate(Date date) and register a special converter somewhere in the chain? (don't know if it's possible)

  3. Create a String variable, a setDate(String date) and let the conversion/validation to the validate method in the ActionForm bean?

Or another approach?

Also, if you have any advice to get up to speed with this framework, I would really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-11-30 19:55:47

在回答您的问题之前,我首先要说的是:人们不了解 ActionForm 是什么或 ActionForm 做什么

ActionForm 表示用户在 HTML 表单中填写的数据。 Struts 读取请求参数并按名称将它们与提交的目标 Action 的已配置 ActionForm 进行匹配。它是用户输入的数据,简单明了。

请求中出现的数据始终为 java.lang.String 类型。但是人们可能有一个名为“age”的表单字段,它是模型数据中的一个 int 。或者也许他们有一个“birthDate”而不是“age”,当然它是模型数据中的 java.util.Date 。因此他们认为将 int 和 Date 类型放置在 ActionForm 上并允许 Struts 将根据请求到达的 String 转换为 int 和 Date 是一个好主意。

这是一个非常有用的转换,作为开发人员,您不必处理它,Struts 可以处理。这有点框架的魔力。

但这不是哈利波特! 整数和日期的转换可能会失败。为什么?

int 是一种原始类型,因此它必须始终有一个值。默认初始化为零。当进行绑定(请求参数到 ActionForm 对象属性)时,Struts 在 ActionForm 上看到一个 int 类型,并尝试将请求 String 值转换为 int。

如果用户插入字符串“5”,则该字段设置为 5。没问题!

但是如果用户插入“bla”怎么办?我们会遇到异常吗?没有!我们返回零值,因为转换(无提示)失败。 Ups!

日期又是一个问题。为什么?因为它们的值是根据请求以字符串形式到达的。它们的格式可能很简单,例如“12/01/2011”,这作为信息完全没有用。为什么?因为表示为字符串的日期必须与 Locale 以便转换为它所代表的正确 Date 实例。

"12/01/2011" + Locale.US = 01 December 2011
"12/01/2011" + Locale.FRENCH = 12 January 2011

Ups!

好的,这就是介绍! 现在我们来回答你的问题

  1. 我应该创建一个 Date 变量并创建一个将发生转换的 setDate(String date) 方法吗?

在某些时候,您必须将日期发送回视图和 Struts html 标签 通常必须使用返回字符串的getDate()。如果 getter 返回 Date(Struts 将执行 toString()吸气剂值)。因此,您实际上需要具有 Date 类型的 setter/getter 和 String 类型的 setter/getter 的 Date 字段。在 Action 类中使用 Date 类型,并使用 String 与视图标签进行交互。

问题?如何在 ActionForm 中处理正确的 Locale 值?

  • 创建一个 Date 变量、一个 setDate(Date date) 并在链中的某个位置注册一个特殊的转换器(不知道是否可能)
  • 这是可能的。您可以创建并注册一个自定义转换器,该转换器可能采用日期的字符串表示形式并将其转换为日期。如果您使用 ISO 8601 格式,我认为您会安全。

    问题?您能否将其适应现有应用程序,而不破坏使用各种格式或区域设置以自己的方式将字符串转换为日期的代码?

  • 创建一个 String 变量,一个 setDate(String date) 并让转换/验证到 actionForm bean 中的 validate 方法
  • 这是行不通的。 validate 方法在 ActionForm 对象上的参数绑定之后调用。当你到达这个地步时就已经晚了。 Struts 完成了转换。如果您有一个值为 0 的 int 类型字段,则无法知道用户是否实际插入了零并且转换有效,或者用户是否插入了“bla”并且转换失败并且您返回了零作为默认初始化值。如果零对于您的应用程序来说是有效值,那么您的麻烦就更大了。

    那么约定是什么?

    没有约定。使用上述信息并根据您的情况应用常识。

    理想情况下,您应该将 ActionForm 中的所有属性都作为字符串,以便在绑定过程中不会丢失任何信息。但这需要在使用属性之前手动将它们转换为 Action 类中的正确类型。您拥有完全的控制权(Struts 不再进行转换,因为源值和目标值都是 String 类型),但您还需要编写大量样板代码来以正确的方式进行转换,这在某些时候可能会变得烦人。

    PS 在我结束这篇文章并上床睡觉之前(在我的国家现在是凌晨 01:00 :D),我只想提一件人们经常看不到的事情。 ActionForm 不是模型的一部分,它也不应该直接与模型交互。

    如果您需要在模型中处理 ActionForm 中的数据,请将其映射为与模型 DTO(数据传输对象)的一对一关系。如果不这样做,那么您就在模型和 Struts 框架之间创建了紧密耦合,因为您的 ActionForm 对象不是 POJO。您的类必须从我们一直在讨论的 Struts 中扩展 ActionForm 类。人们不会这样做,而是将 ActionForm 直接发送给模型。更糟糕的是,他们还使用 validate 方法来执行业务验证,而不是诸如“是否需要”、“值是否在范围内”等基本验证。ActionForm

    只是 ActionForm 之间的通信路径。动作(控制器)和视图。就这样对待它。

    Before I respond to your question, let me start by saying this: People don't understand what ActionForm is or what ActionForm does

    The ActionForm represents the data that the user filled in the HTML form. Struts reads in the request parameters and matches them by name with the configured ActionForm for the target Action of the submit. It is data inputted by the user, plain and simple.

    Data that comes on the request is always of type java.lang.String. But people might have a form field named "age" which is an int in their Model data. Or maybe instead of "age" they have a "birthDate" which off course is java.util.Date in their Model data. So they think it is a good idea to have the type int and Date placed on the ActionForm and allow Struts to convert the Strings that arrive on request into ints and Dates.

    Its a very useful conversion and you as a developer don't have to handle it, Struts does. It's a little bit of framework magic.

    But this ain't freaking Harry Potter! Conversion can fail on ints and Dates. Why?

    int is a primitive type and as such it must always have a value. Default initialization is with zero. When doing the binding (request parameters to ActionForm object properties) Struts sees an int type on the ActionForm and tries to convert the request String value to int.

    If user inserted String "5", the field is set to 5. That's fine!

    But what if user inserted "bla"? Will we get an exception thrown in our face? Nope! We get back a value of zero because conversion (silently) failed. Ups!

    Dates again are an issue. Why? Because their value arrives on request as String. Their format might be something simple like "12/01/2011" which is completely useless as information. Why? Because Dates represented as Strings must go hand in hand with a Locale in order to be transformed to the correct Date instance it represents.

    "12/01/2011" + Locale.US = 01 December 2011
    "12/01/2011" + Locale.FRENCH = 12 January 2011
    

    Ups!

    Ok, so this was the intro! Now let's go to your question.

    1. Should I create a Date variable and create a setDate(String date) method where the conversion will occur.

    At some point you will have to send the Date back to the view and the Struts html tags will normally have to go for a getDate() which returns String. The "12/01/2011" you get on user input might be displayed as "Jan. 12, 2011 00:00:00" if the getter returns Date (Struts will do a toString() on the getter value). So you actually need the Date field with both a setter/getter of type Date and a setter/getter of type String. Use type Date in your Action class and use String for interfacing with the view tags.

    Question? How do you get handle on the proper Locale value in your ActionForm?

    1. Create a Date variable, a setDate(Date date) and register a special converter somewhere in the chain (don't know if it's possible)

    It is possible. You can create and register a custom converter that might take String representations of dates and convert them to Date. If you use the ISO 8601 format I think you will be safe.

    Question? Can you accommodate this into the existing application without breaking code that transforms String to dates in their own way by using all sorts of formats or Locales?

    1. Create a String variable, a setDate(String date) and let the conversion/validation to the validate method in the actionForm bean

    This won't work. The validate method is called after the parameter bindings on the ActionForm object. When you reach this point is already to late. Struts did the conversion. If you have a field of type int with value zero there is no way to know if the user actually inserted zero and conversion worked or if the user inserted "bla" and the conversion failed and you got back zero as default initialization value. If zero is a valid value for your application than you are in even bigger trouble.

    So what's the convention?

    There is no convention. Use the above information and apply common sense given your situation.

    Ideally you should have all properties in the ActionForm as Strings so that you loose no information during the binding. But this involves manually converting the properties to the proper type in the Action class, before using them. You have full control (Struts no longer does conversions since source and destination values are of type String) but you also have a lot of boiler plate code to write to do it the proper way which at some point can become annoying.

    P.S. Before I end this and go to bed (it's 01:00 AM in my Country :D) I just want to mention one thing that people often don't see. The ActionForm is not part of the model, neither should it interact directly with the model.

    If you need the data from the ActionForm to be processed in the model, then map it as a one-to-one relationships with a Model DTO (data transfer object). If you don't, then you create a tight coupling between the Model and the Struts framework because your ActionForm objects are not a POJOs. Your class must extend the ActionForm class from Struts that we've been talking about. People don't do this and send the ActionForm straight to the Model. What is even worse is that they also use the validate method to perform business validations instead of basic validation such as "is required", "is value withing range" etc.

    ActionForms are just a communication path between the Action (controller) and the view. Treat it as such.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文