Date Conversion in Struts2 [closed]

自闭症网瘾萝莉.ら 提交于 2019-11-27 23:23:22
Andrea Ligios

Struts2 Type Conversion

Built in Type Conversion Support

Type Conversion is implemented by XWork.

XWork will automatically handle the most common type conversion for you. This includes support for converting to and from Strings for each of the following:

String
boolean / Boolean
char / Character
int / Integer, float / Float, long / Long, double / Double
dates - uses the SHORT format for the Locale associated with the current request
arrays - assuming the individual strings can be coverted to the individual items
collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created

SHORT format in JAVA:

Style     U.S. Locale   French Locale

SHORT       6/30/09       30/06/09

This means that it already works, but in SHORT format only, and it is not configurable. Try it.

Then you can alter the value clientside with some javascript hack before sending it, or copy and paste this small converter, or use a jQuery datetimepicker (recommended), and your only problem will be which theme is the coolest :)


EDIT

After some crypto comment, I've tried and obviously it's like the documentations states.

  • If your Locale is (eg.) en_US, you need to send a String data in the format MM/dd/yy.
  • If your Locale is (eg.) it_IT, you need to send a String data in the format dd/MM/yy.
  • If you input dd/MM/yy with en_US Locale, you will get validation error and INPUT result.
  • If you try using - instead of /, it will fail the same.
  • If you input a date in another format, eg. dd/MM/yyyy (different from SHORT but right for your Locale), your date will be correctly set the same.
  • If you read your Action date in the JSP without formatting it, it will be always displayed in SHORT.

Then, as said above, if you need to let the user input the date manually, tell the user to respect the right format for your Locale (eg.

<label>Input a date (dd/mm/yyyy):</label> 
<s:textfield name="aDate" />
<s:fielderror fieldName="aDate" />

Or alter it through javascript after inserted, or use a custom converter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!