Struts2 Datetime picker displayformat issue

这一生的挚爱 提交于 2019-12-08 06:26:22

问题


A issue I am facing with struts2.0.14's date time picker tag

The problem is that struts2 datetimepicker displayformat attribute must always be set to format of tomcat server date time format else the submitted values are null.

Change Date time setting by in Win 7:

  1. RightClick bottom right corner date.
  2. Click on Change Date & Time Settings
  3. Change calender settings
  4. Change Regional Settings
  5. Set Format as English(India)

Repro Steps

  1. Change the Regional Settings as mentioned above & restart the tomcat server.
  2. Now do not use any displayformat or use display format other than "dd/mm/yyyy" in date time picker
  3. Submit the struts2 form with date 21/12/2012
  4. In Action submitted date is set to null
  5. Now change the regional setting to English(US) and do not use any displayformat and restart the server.
  6. Values in action are set as submitted through form.

Expected result

  1. Whatever is the system date time format then date must be parsed accordingly and made available in action.

Envi:

Java 6, Struts2.0.14, Firefix 12, tomcat 6.

Any workarounds or fixes through properties or something?

*Note: Do not answer if the answer is to use jquery or some other js lib or an upgrade.


回答1:


You need to use your custom DateTime converter.

In your xwork-conversion.properties (create if you don't have one) file put this line:

java.util.Date = org.yourproject.common.StringToDateTimeConverter

And in your StringToDateTimeConverter, have code something like this

import java.util.*
import java.text.DateFormat.*;

    public class StringToDateTimeConverter extends StrutsTypeConverter{

        public Object convertFromString(Map context, String[] strings, Class toClass) {     

            DateFormat DATETIME_FORMAT = getDateInstance(SHORT, Locale.getDefault());

            if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) {
                return null;
            }

            try
            {
                DATETIME_FORMAT.setLenient(false);
                return DATETIME_FORMAT.parseObject(strings[0]);

            } catch (ParseException e) {
                //throw new TypeConversionException(); <-- if you want to catch conversion error
                return null;
            }
        }

        public String convertToString(Map context, Object date) {
            DateFormat DATETIME_FORMAT = getDateInstance(SHORT, Locale.getDefault());
            if (date != null && date instanceof Date) {
                return DATETIME_FORMAT.format(date);
            } else {
                return null;
            }
        }
    }

References:

Java Date Format for Locale

http://www.roseindia.net/java/java-get-example/java-get-default-locale.shtml



来源:https://stackoverflow.com/questions/10910026/struts2-datetime-picker-displayformat-issue

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