English 中文(简体)
输入日期( Struts2)
原标题:Input Date in Struts2
  • 时间:2012-05-25 01:03:25
  •  标签:
  • struts2

如何改变 java. util. Date 对象在 struts 2. 我测试过的东西, Struggs 2 接受 m/ d/yy。 是否可以修改格式?

如何更改默认消息“ 字段日期的无效字段值 ”?

在 struts2 中的日期标记只能用于输出 。

问题回答

您可以通过格式到 s: date 标签

<s:date name="myDate" format="yyyy-MM-dd" />

要更改默认错误消息, 您可以在 validation. xml 中添加信件参数

   <field name="myDate">
        <field-validator type="date">
            <message key="errors.myDateRequired" />
        </field-validator>
    </field>

然后在您的 ApplicationResources. propertys 文件中,您将为 errors.myDate required 提供价值。

更新:根据评论更新

s:date 只是一个只读标签, 表示无法接受用户输入 。

为使案文能够以不同格式接受日期,有几种办法:

  1. Use struts2-jQuery plugin, it has a very nice date picker which support different date timeformat, showcase, DatePickerTag.
  2. Custom javascript code to format user input.
  3. Use jQuery (if allowed) and use jQuery datepicker

您可以使用自定义转换器来使用日期的文本字段, < a href="https://stackoverflow.com/ questions/10750991/ no semethod expecation- in-struts2//10751705#107517051705" >example

Struts 接受的日期格式取决于用户的本地化 。 因此, 对于不同的本地化, 您可以有不同的本地化格式 。 这样您将无法支持同一本地化的多日期格式, 但我不明白为什么您需要这个格式, 它只会混淆您的用户 。 对于没有 Javascript 的同一本地化的本地化, 唯一支持不同日期格式的方法是使用@ mprabhat 便笺的自定义日期转换器 。

你可以看到官方关于这个文件:

定义每种语言的格式 y 资源捆绑。 例如, 适当的 en_ US 格式定义扩展名可能看起来是这样 :

format.date = {0,date,MM/dd/yy}

同时,您可以在您的 de_DE捆绑中添加以下内容:

format.date = {0,date,dd.MM.yyyy}

http://struts.apache.org/2.1.6/docs/formatting-dates-and-numbers.html





相关问题
Getting sitemesh to work with struts2

I m trying to integrate sitemesh into my struts2 app, but i m finding that it s not making any difference and isn t showing any errors (or anything sitemesh related at all) in the logs. I ve started ...

Struts Action Validation

Hey People. I have been recently validating Struts 2 actions. In One of my action class, the save method to be precise must have two id s. That is an assessmenttype id and a course id. this is what is ...

struts2 action not calling properly

On default I want my struts2 app to forward to an action: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"...

How to validate a select tag in struts 2 framework

I am a newbie into programming and i am currently employed as a junior programmer. I am currently having some problems validating the select tags in one of my forms. What i actually trying to do is to ...

Setting the default value in Struts2

I am setting the value(kind of default value) for a drop down select value from action class in a page(given below). When the page loads the value is beig displayed but the other elements of the ...

Localized exceptions (within a Struts2 app)

I am developing a Struts 2 application with support for multiple languages. If one of the domain objects needs to throw an exception, how can it do so in such a way that the error message is no ...

热门标签