提交 a1916ca7 编写于 作者: J Juergen Hoeller

polishing

上级 d6197b74
......@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.format.annotation;
import java.lang.annotation.ElementType;
......@@ -43,7 +44,7 @@ import java.lang.annotation.Target;
* @since 3.0
* @see org.joda.time.format.DateTimeFormat
*/
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface DateTimeFormat {
......@@ -69,6 +70,7 @@ public @interface DateTimeFormat {
*/
String pattern() default "";
/**
* Common ISO date time format patterns.
* @author Keith Donald
......@@ -96,6 +98,6 @@ public @interface DateTimeFormat {
* Indicates that no ISO-based format pattern should be applied.
*/
NONE
}
}
......@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.format.annotation;
import java.lang.annotation.ElementType;
......@@ -36,7 +37,7 @@ import java.lang.annotation.Target;
* @since 3.0
* @see java.text.NumberFormat
*/
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface NumberFormat {
......@@ -54,6 +55,7 @@ public @interface NumberFormat {
*/
String pattern() default "";
/**
* Common number format styles.
* @author Keith Donald
......@@ -75,6 +77,6 @@ public @interface NumberFormat {
* The percent format for the current locale.
*/
PERCENT
}
}
......@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.format.datetime.joda;
import java.util.Calendar;
......@@ -29,6 +30,7 @@ import org.joda.time.LocalTime;
import org.joda.time.ReadableInstant;
import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Parser;
import org.springframework.format.Printer;
......@@ -37,7 +39,9 @@ import org.springframework.format.annotation.DateTimeFormat.ISO;
/**
* Formats fields annotated with the {@link DateTimeFormat} annotation.
*
* @author Keith Donald
* @author Juergen Hoeller
* @since 3.0
* @see DateTimeFormat
*/
......@@ -45,6 +49,7 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
private final Set<Class<?>> fieldTypes;
public DateTimeFormatAnnotationFormatterFactory() {
this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
}
......@@ -53,16 +58,20 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
return this.fieldTypes;
}
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation);
if (ReadableInstant.class.isAssignableFrom(fieldType)) {
return new ReadableInstantPrinter(formatter);
} else if (ReadablePartial.class.isAssignableFrom(fieldType)) {
}
else if (ReadablePartial.class.isAssignableFrom(fieldType)) {
return new ReadablePartialPrinter(formatter);
} else if (Calendar.class.isAssignableFrom(fieldType)) {
}
else if (Calendar.class.isAssignableFrom(fieldType)) {
// assumes Calendar->ReadableInstant converter is registered
return new ReadableInstantPrinter(formatter);
} else {
}
else {
// assumes Date->Long converter is registered
return new MillisecondInstantPrinter(formatter);
}
......@@ -72,6 +81,7 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
return new DateTimeParser(configureDateTimeFormatterFrom(annotation));
}
// internal helpers
private Set<Class<?>> createFieldTypes() {
......@@ -90,9 +100,11 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
private DateTimeFormatter configureDateTimeFormatterFrom(DateTimeFormat annotation) {
if (!annotation.pattern().isEmpty()) {
return forPattern(annotation.pattern());
} else if (annotation.iso() != ISO.NONE) {
return forISO(annotation.iso());
} else {
}
else if (annotation.iso() != ISO.NONE) {
return forIso(annotation.iso());
}
else {
return forStyle(annotation.style());
}
}
......@@ -101,12 +113,14 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
return org.joda.time.format.DateTimeFormat.forPattern(pattern);
}
private DateTimeFormatter forISO(ISO iso) {
private DateTimeFormatter forIso(ISO iso) {
if (iso == ISO.DATE) {
return org.joda.time.format.ISODateTimeFormat.date();
} else if (iso == ISO.TIME) {
}
else if (iso == ISO.TIME) {
return org.joda.time.format.ISODateTimeFormat.time();
} else {
}
else {
return org.joda.time.format.ISODateTimeFormat.dateTime();
}
}
......@@ -115,4 +129,4 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
return org.joda.time.format.DateTimeFormat.forStyle(style);
}
}
\ No newline at end of file
}
......@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.format.datetime.joda;
import java.util.Calendar;
......@@ -26,19 +27,22 @@ import org.joda.time.ReadableInstant;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.Parser;
import org.springframework.format.Printer;
/**
* Configures Joda Time's Formatting system for use with Spring.
*
* @author Keith Donald
* @author Juergen Hoeller
* @since 3.0
* @see #setDateStyle(String)
* @see #setTimeStyle(String)
* @see #setDateTimeStyle(String)
* @see #setUseISOFormat(boolean)
* @see #installJodaTimeFormatting(FormatterRegistry)
* @see #setDateStyle
* @see #setTimeStyle
* @see #setDateTimeStyle
* @see #setUseIsoFormat
* @see #installJodaTimeFormatting
*/
public class JodaTimeFormattingConfigurer {
......@@ -48,12 +52,12 @@ public class JodaTimeFormattingConfigurer {
private String dateTimeStyle;
private boolean useISOFormat;
private boolean useIsoFormat;
/**
* Set the default format style of Joda {@link LocalDate} objects.
* Default is {@link DateTimeFormat#shortDate()}.
* @param dateStyle the date format style
*/
public void setDateStyle(String dateStyle) {
this.dateStyle = dateStyle;
......@@ -62,16 +66,15 @@ public class JodaTimeFormattingConfigurer {
/**
* Set the default format style of Joda {@link LocalTime} objects.
* Default is {@link DateTimeFormat#shortTime()}.
* @param timeStyle the time format style
*/
public void setTimeStyle(String timeStyle) {
this.timeStyle = timeStyle;
}
/**
* Set the default format style of Joda {@link LocalDateTime} and {@link DateTime} objects, as well as JDK {@link Date} and {@link Calendar} objects.
* Set the default format style of Joda {@link LocalDateTime} and {@link DateTime} objects,
* as well as JDK {@link Date} and {@link Calendar} objects.
* Default is {@link DateTimeFormat#shortDateTime()}.
* @param dateTimeStyle the date time format style
*/
public void setDateTimeStyle(String dateTimeStyle) {
this.dateTimeStyle = dateTimeStyle;
......@@ -81,27 +84,30 @@ public class JodaTimeFormattingConfigurer {
* Set whether standard ISO formatting should be applied to all Date/Time types.
* Default is false (no).
* If set to true, the dateStyle, timeStyle, and dateTimeStyle properties are ignored.
* @param useISOFormat true to enable ISO formatting
*/
public void setUseISOFormat(boolean useISOFormat) {
this.useISOFormat = useISOFormat;
public void setUseIsoFormat(boolean useIsoFormat) {
this.useIsoFormat = useIsoFormat;
}
/**
* Install Joda Time formatters given the current configuration of this {@link JodaTimeFormattingConfigurer}.
*/
public void installJodaTimeFormatting(FormatterRegistry formatterRegistry) {
JodaTimeConverters.registerConverters(formatterRegistry.getConverterRegistry());
JodaTimeConverters.registerConverters(formatterRegistry);
DateTimeFormatter jodaDateFormatter = getJodaDateFormatter();
formatterRegistry.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(jodaDateFormatter), new DateTimeParser(jodaDateFormatter));
formatterRegistry.addFormatterForFieldType(LocalDate.class,
new ReadablePartialPrinter(jodaDateFormatter), new DateTimeParser(jodaDateFormatter));
DateTimeFormatter jodaTimeFormatter = getJodaTimeFormatter();
formatterRegistry.addFormatterForFieldType(LocalTime.class, new ReadablePartialPrinter(jodaTimeFormatter), new DateTimeParser(jodaTimeFormatter));
formatterRegistry.addFormatterForFieldType(LocalTime.class,
new ReadablePartialPrinter(jodaTimeFormatter), new DateTimeParser(jodaTimeFormatter));
DateTimeFormatter jodaDateTimeFormatter = getJodaDateTimeFormatter();
Parser<DateTime> dateTimeParser = new DateTimeParser(jodaDateTimeFormatter);
formatterRegistry.addFormatterForFieldType(LocalDateTime.class, new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser);
formatterRegistry.addFormatterForFieldType(LocalDateTime.class,
new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser);
Printer<ReadableInstant> readableInstantPrinter = new ReadableInstantPrinter(jodaDateTimeFormatter);
formatterRegistry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser);
......@@ -111,37 +117,42 @@ public class JodaTimeFormattingConfigurer {
formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
}
// internal helpers
private DateTimeFormatter getJodaDateFormatter() {
if (this.useISOFormat) {
if (this.useIsoFormat) {
return ISODateTimeFormat.date();
}
if (this.dateStyle != null) {
return DateTimeFormat.forStyle(this.dateStyle + "-");
} else {
}
else {
return DateTimeFormat.shortDate();
}
}
private DateTimeFormatter getJodaTimeFormatter() {
if (this.useISOFormat) {
if (this.useIsoFormat) {
return ISODateTimeFormat.time();
}
if (this.timeStyle != null) {
return DateTimeFormat.forStyle("-" + this.timeStyle);
} else {
}
else {
return DateTimeFormat.shortTime();
}
}
private DateTimeFormatter getJodaDateTimeFormatter() {
if (this.useISOFormat) {
if (this.useIsoFormat) {
return ISODateTimeFormat.dateTime();
}
if (this.dateTimeStyle != null) {
return DateTimeFormat.forStyle(this.dateTimeStyle);
} else {
}
else {
return DateTimeFormat.shortDateTime();
}
}
......
......@@ -18,23 +18,21 @@ package org.springframework.web.servlet.tags;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.springframework.util.StringUtils;
import org.springframework.web.util.ExpressionEvaluationUtils;
import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.JavaScriptUtils;
import org.springframework.web.util.TagUtils;
import org.springframework.web.util.UriUtils;
import org.springframework.util.StringUtils;
/**
* JSP tag for creating URLs. Modeled after the JSTL c:url tag with backwards
......@@ -44,7 +42,7 @@ import org.springframework.util.StringUtils;
* <ul>
* <li>URL encoded template URI variables</li>
* <li>HTML/XML escaping of URLs</li>
* <li>JavaScipt escaping of URLs</li>
* <li>JavaScript escaping of URLs</li>
* </ul>
*
* <p>Template URI variables are indicated in the {@link #setValue(String) 'value'}
......@@ -235,13 +233,11 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
* @param includeQueryStringDelimiter true if the query string should start
* with a '?' instead of '&'
* @return the query string
* @throws JspException
*/
protected String createQueryString(List<Param> params, Set<String> usedParams, boolean includeQueryStringDelimiter)
throws JspException {
String encoding = pageContext.getResponse().getCharacterEncoding();
StringBuilder qs = new StringBuilder();
for (Param param : params) {
if (!usedParams.contains(param.getName()) && StringUtils.hasLength(param.getName())) {
......@@ -274,12 +270,11 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
* @param params parameters used to replace template markers
* @param usedParams set of template parameter names that have been replaced
* @return the URL with template parameters replaced
* @throws JspException
*/
protected String replaceUriTemplateParams(String uri, List<Param> params, Set<String> usedParams)
throws JspException {
String encoding = pageContext.getResponse().getCharacterEncoding();
String encoding = pageContext.getResponse().getCharacterEncoding();
for (Param param : params) {
String template = URL_TEMPLATE_DELIMITER_PREFIX + param.getName() + URL_TEMPLATE_DELIMITER_SUFFIX;
if (uri.contains(template)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册