
|
If you were logged in you would be able to see more operations.
|
|
|
Struts 1
Created: 23/Dec/03 08:01 AM
Updated: 03/Jul/07 10:29 PM
|
|
| Component/s: |
Tag Libraries
|
| Affects Version/s: |
1.1.0
|
| Fix Version/s: |
None
|
|
|
Environment:
|
Operating System: other
Platform: All
Operating System: other
Platform: All
|
|
|
currently as of version 1.2 of Struts custom tag library, timezone is not
configurable for using bean:write tag with Date object. Both JSTL and apache
date custom tags support converting date value to that of different timezone.
below is a subclass I created to accomodate timezone configuration. Something
like this would do..
public class WriteDateTag extends WriteTag {
private String timeZoneString;
public void setTimezone(String timeZoneString) {
this.timeZoneString = timeZoneString;
}
public String getTimezoneString() {
return timeZoneString;
}
/**
* Format value according to specified format string (as tag attribute or
* as string from message resources) or to current user locale.
*
* @param valueToFormat value to process and convert to String
* @exception JspException if a JSP exception has occurred
*/
protected String formatValue(Object valueToFormat) throws JspException {
Format format = null;
Object value = valueToFormat;
Locale locale =
RequestUtils.retrieveUserLocale(pageContext, this.localeKey);
boolean formatStrFromResources = false;
String formatString = formatStr;
// Try to retrieve format string from resources by the key from formatKey.
if( ( formatString==null ) && ( formatKey!=null ) ) {
formatString = retrieveFormatString( this.formatKey );
if( formatString!=null )
formatStrFromResources = true;
}
if (formatString == null) {
if (value instanceof java.sql.Timestamp) {
formatString = retrieveFormatString(SQL_TIMESTAMP_FORMAT_KEY);
} else if (value instanceof java.sql.Date) {
formatString = retrieveFormatString(SQL_DATE_FORMAT_KEY);
} else if (value instanceof java.sql.Time) {
formatString = retrieveFormatString(SQL_TIME_FORMAT_KEY);
} else if (value instanceof java.util.Date) {
formatString = retrieveFormatString(DATE_FORMAT_KEY);
}
if (formatString != null)
formatStrFromResources = true;
}
TimeZone tz = null;
if (timeZoneString != null) {
tz = TimeZone.getTimeZone(timeZoneString);
} else {
tz = TimeZone.getDefault();
}
if (formatString != null) {
if (formatStrFromResources) {
format = new SimpleDateFormat(formatString, locale);
} else {
format = new SimpleDateFormat(formatString);
}
}
if (format != null) {
((SimpleDateFormat) format).setTimeZone(tz);
return format.format(value);
} else {
return value.toString();
}
}
}
|
|
Description
|
currently as of version 1.2 of Struts custom tag library, timezone is not
configurable for using bean:write tag with Date object. Both JSTL and apache
date custom tags support converting date value to that of different timezone.
below is a subclass I created to accomodate timezone configuration. Something
like this would do..
public class WriteDateTag extends WriteTag {
private String timeZoneString;
public void setTimezone(String timeZoneString) {
this.timeZoneString = timeZoneString;
}
public String getTimezoneString() {
return timeZoneString;
}
/**
* Format value according to specified format string (as tag attribute or
* as string from message resources) or to current user locale.
*
* @param valueToFormat value to process and convert to String
* @exception JspException if a JSP exception has occurred
*/
protected String formatValue(Object valueToFormat) throws JspException {
Format format = null;
Object value = valueToFormat;
Locale locale =
RequestUtils.retrieveUserLocale(pageContext, this.localeKey);
boolean formatStrFromResources = false;
String formatString = formatStr;
// Try to retrieve format string from resources by the key from formatKey.
if( ( formatString==null ) && ( formatKey!=null ) ) {
formatString = retrieveFormatString( this.formatKey );
if( formatString!=null )
formatStrFromResources = true;
}
if (formatString == null) {
if (value instanceof java.sql.Timestamp) {
formatString = retrieveFormatString(SQL_TIMESTAMP_FORMAT_KEY);
} else if (value instanceof java.sql.Date) {
formatString = retrieveFormatString(SQL_DATE_FORMAT_KEY);
} else if (value instanceof java.sql.Time) {
formatString = retrieveFormatString(SQL_TIME_FORMAT_KEY);
} else if (value instanceof java.util.Date) {
formatString = retrieveFormatString(DATE_FORMAT_KEY);
}
if (formatString != null)
formatStrFromResources = true;
}
TimeZone tz = null;
if (timeZoneString != null) {
tz = TimeZone.getTimeZone(timeZoneString);
} else {
tz = TimeZone.getDefault();
}
if (formatString != null) {
if (formatStrFromResources) {
format = new SimpleDateFormat(formatString, locale);
} else {
format = new SimpleDateFormat(formatString);
}
}
if (format != null) {
((SimpleDateFormat) format).setTimeZone(tz);
return format.format(value);
} else {
return value.toString();
}
}
} |
Show » |
|
maintenance mode and are not actively enhanced. The best solution is to use the
JSTL.