Hello Bruce,
On Nov 16, 2007 7:03 AM, BruceLee <[EMAIL PROTECTED]> wrote:
> Dear Team,
>
> I digged days, tried to search on this forum, got some good info for my
> question, but still puzzles. We use 1.9.4 with WebWork.
>
> We want default conversion from string on JSP to Date in model using Appfuse
> DateConverter. I want to use fixed format yyyy-MM-dd. Then I set
> ApplicationResource.properties:
> data.format=yyyy-MM-dd.
>
> In JSP I have something like
> <input name="agent.beginDate" type="text" value="" size="10"
> id="beginDate">
>
> In model Agent.java
> ...
> private Date beginDate;
> public void setBeginDate(Date beginDate) {
> this.beginDate = beginDate;
> }
>
> However, the web page doesn't accept yyyy-MM-dd like 2007-01-01; only
> accepts MM/dd/yyyy which is my web server's locale date format.
>
> We also tried to add a file WEB-INF/classes/xwork-conversion.properties:
> java.util.Date=DateConverter
> //or java.util.Date=com.myorg.util.DateConverter
> But it didn't help.
>
> Could you help give some ideas?
> Thanks!
> -Bruce
Here are my steps: create the new converter in src/web (it has to be
child class of OGNL converter):
import java.lang.reflect.Member;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import ognl.DefaultTypeConverter;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.util.DateUtil;
public class DateConverter extends DefaultTypeConverter {
static final Map<String, DateFormat> formaterCache = new
HashMap<String, DateFormat>();
private final static Log log = LogFactory.getLog(DateConverter.class);
public Object convertValue(Map map, Object object, Class aClass) {
Object o = null;
if (object instanceof Object[]) {
if (((Object[]) object).length > 0) {
o = ((Object[]) object)[0];
}
} else {
o = object;
}
if (o == null) {
return null;
} else if (aClass == Timestamp.class) {
return convertToDate(aClass, o,
DateUtil.getDateTimePattern());
} else if (aClass == Time.class) {
return convertToDate(aClass, o,
DateUtil.getTimePattern());
} else if (aClass == Date.class || aClass ==
java.sql.Date.class) {
return convertToDate(aClass, o,
DateUtil.getDatePattern());
} else if (aClass == String.class) {
return convertToString(o);
}
throw new ConversionException("Could not convert " +
o.getClass().getName() + " to " +
aClass.getName());
}
public Object convertValue(Map map, Object o, Member member, String s,
Object o1, Class aClass) {
return this.convertValue(map, o1, aClass);
}
protected Object convertToDate(Class type, Object value, String
pattern) {
if (log.isDebugEnabled()) {
log.debug("Converting " + value + " to " + type + "
using pattern "
+ pattern);
}
DateFormat df = getFormater(pattern);
if (value instanceof String) {
try {
if (StringUtils.isEmpty(value.toString())) {
return null;
}
Date date = df.parse((String) value);
if (type != Date.class) {
//handling of java.sql.* (Timestamp,
Time, Date)
date = (Date) type.getConstructor(new
Class[]
{long.class}).newInstance(new Long(date.getTime()));
}
return date;
} catch (Exception pe) {
pe.printStackTrace();
throw new ConversionException("Error converting
String to Date");
}
}
throw new ConversionException("Could not convert "
+ value.getClass().getName() + " to " + type.getName());
}
protected Object convertToString(Object value) {
if (log.isDebugEnabled()) {
log.debug("Converting " + value + " to String");
}
if (value instanceof Date) {
DateFormat df;
if (value instanceof Timestamp) {
df = getFormater(DateUtil.getDateTimePattern());
} else if (value instanceof Time) {
df = getFormater(DateUtil.getTimePattern());
} else {
df = getFormater(DateUtil.getDatePattern());
}
try {
return df.format(value);
} catch (Exception e) {
e.printStackTrace();
throw new ConversionException("Error converting
Date to String");
}
}
return value.toString();
}
/**
* Returns DateFormat object from cache.
*
* @param pattern
* date/time formatting pattern
* @return DateFormat
*/
protected DateFormat getFormater(final String pattern) {
synchronized (formaterCache) {
DateFormat tmpResult = formaterCache.get(pattern);
if (tmpResult == null) {
tmpResult = new SimpleDateFormat(pattern);
formaterCache.put(pattern, tmpResult);
}
return tmpResult;
}
}
}
I've also changed DateUtil class a little bit:
private static String getPatternFromResources(final String aKey,
final String aDefault) {
final Locale locale = LocaleContextHolder.getLocale();
String pattern = null;
try {
pattern = ResourceBundle.getBundle(Constants.BUNDLE_KEY, locale)
.getString(aKey);
} catch (MissingResourceException mse) {
if (log.isWarnEnabled()) {
log.warn("No key '" + aKey + "' in resources for " +
locale);
}
}
if (pattern==null || "".equals(pattern.trim())) {
pattern = aDefault;
}
if (log.isDebugEnabled()) {
log.debug("DateFormat pattern for locale '" + locale + "' is
'" + pattern + "'");
}
return pattern;
}
/**
* Return date pattern for locale of current thread (default is dd.MM.yyyy).
* You can configure it by resource key 'date.format'.
* @return a string representing the date pattern on the UI
*/
public static String getDatePattern() {
return getPatternFromResources("date.format", "dd.MM.yyyy");
}
/**
* Return time pattern for locale of current thread (default is HH:mm).
* You can configure it by resource key 'time.format'.
* @return a string representing the date pattern on the UI
*/
public static String getTimePattern() {
return getPatternFromResources("time.format", "HH:mm");
}
/**
* Return dateTime pattern for locale of current thread (default
is getDatePattern() + "HH:mm:ss").
* You can configure it by resource key 'datetime.format'.
* @return a string representing the date pattern on the UI
* @see #getDatePattern()
*/
public static String getDateTimePattern() {
return getPatternFromResources("datetime.format",
getDatePattern() + "HH:mm:ss");
}
next step as you mentioned is to register your converter in XWork
(create file web/WEB-INF/classes/xwork-conversion.properties) with the
following line:
java.util.Date=your.package.DateConverter
and finally set your date preferences in ApplicationResources*.properties eg:
date.format=dd/MM/yyyy
time.format=HH:mm
datetime.format=dd/MM/yyyy HH:mm:ss
It should work now.
Regards,
-- josef cacek
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]