diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java index 12297ff3d50..4dcef335546 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java @@ -18,17 +18,11 @@ package org.springframework.ui.alert; /** * Communicates an event of interest to the user. * For example, an alert may inform a user of a web application a business rule was violated. - * TODO - should we introduce detail messages here * @author Keith Donald * @since 3.0 */ public interface Alert { - /** - * The user interface element this alert is associated with; for example, "registration.password" - */ - public String getElement(); - /** * The code uniquely identifying this kind of alert; for example, "weakPassword". * May be used as a key to lookup additional alert details. diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java index 44554463e3a..e9ef14665b1 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java @@ -41,8 +41,9 @@ public interface AlertContext { /** * Add an alert to this context. + * @param the element this alert is associated with * @param alert the alert to add */ - public void add(Alert alert); + public void add(String element, Alert alert); -} +} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/Alerts.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alerts.java new file mode 100644 index 00000000000..22aa1eee0de --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alerts.java @@ -0,0 +1,111 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ui.alert; + +/** + * A static factory for conveniently constructing Alerts. + * Usage example: + *
+ *    static import org.springframework.ui.alert.Alerts;
+ *    
+ *    public void example() {
+ *        info("An info alert");
+ *        warning("A warning alert");
+ *        error("An error alert");
+ *        fatal("A fatal alert");
+ *    }
+ * 
+ * @author Keith Donald + * @since 3.0 + */ +public final class Alerts { + + /** + * Creates a new info alert. + * @param message the alert message + * @return the info alert + * @see Severity#INFO + */ + public static Alert info(String message) { + return new GenericAlert(Severity.INFO, null, message); + } + + /** + * Creates a new warning alert. + * @param message the alert message + * @return the info alert + * @see Severity#WARNING + */ + public static Alert warning(String message) { + return new GenericAlert(Severity.WARNING, null, message); + } + + /** + * Creates a new error alert. + * @param message the alert message + * @return the info alert + * @see Severity#ERROR + */ + public static Alert error(String message) { + return new GenericAlert(Severity.ERROR, null, message); + } + + /** + * Creates a new fatal alert. + * @param message the alert message + * @return the info alert + * @see Severity#ERROR + */ + public static Alert fatal(String message) { + return new GenericAlert(Severity.FATAL, null, message); + } + + private static class GenericAlert implements Alert { + + private Severity severity; + + private String code; + + private String message; + + public GenericAlert(Severity severity, String code, String message) { + this.severity = severity; + this.code = code; + this.message = message; + } + + public Severity getSeverity() { + return severity; + } + + public String getCode() { + return code; + } + + public String getMessage() { + return message; + } + + public String toString() { + if (getCode() != null) { + return getCode() + " - " + getMessage(); + } else { + return getMessage(); + } + } + } + +} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java index 7d3880b6255..90187b38a51 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java @@ -54,8 +54,8 @@ public class DefaultAlertContext implements AlertContext { return Collections.unmodifiableList(messages); } - public void add(Alert alert) { - List alerts = this.alerts.get(alert.getElement()); + public void add(String element, Alert alert) { + List alerts = this.alerts.get(element); alerts.add(alert); } diff --git a/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericBinder.java b/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericBinder.java index ca3b814b2f3..fe15699ff65 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericBinder.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/binding/support/GenericBinder.java @@ -55,6 +55,7 @@ import org.springframework.ui.binding.Binder; import org.springframework.ui.binding.Binding; import org.springframework.ui.binding.BindingResult; import org.springframework.ui.binding.BindingResults; +import org.springframework.ui.format.AnnotationFormatterFactory; import org.springframework.ui.format.Formatter; import org.springframework.ui.message.MessageBuilder; import org.springframework.ui.message.ResolvableArgument; @@ -176,6 +177,26 @@ public class GenericBinder implements Binder { return binding; } + /** + * Register a Formatter to format the model properties of a specific property type. + * Convenience method that calls {@link FormatterRegistry#add(Class, Formatter)} internally. + * The type may be a marker annotation type; if so, the Formatter will be used on properties having that marker annotation. + * @param propertyType the model property type + * @param formatter the formatter + */ + public void registerFormatter(Class propertyType, Formatter formatter) { + formatterRegistry.add(propertyType, formatter); + } + + /** + * Register a FormatterFactory that creates Formatter instances as required to format model properties annotated with a specific annotation. + * Convenience method that calls {@link FormatterRegistry#add(AnnotationFormatterFactory)} internally. + * @param factory the formatter factory + */ + public void registerFormatterFactory(AnnotationFormatterFactory factory) { + formatterRegistry.add(factory); + } + public Binding getBinding(String property) { Binding binding = bindings.get(property); if (binding == null && !strict) { @@ -528,10 +549,6 @@ public class GenericBinder implements Binder { public Alert getAlert() { return new AbstractAlert() { - public String getElement() { - return getProperty(); - } - public String getCode() { return "noSuchBinding"; } @@ -581,10 +598,6 @@ public class GenericBinder implements Binder { public Alert getAlert() { return new AbstractAlert() { - public String getElement() { - return getProperty(); - } - public String getCode() { return "invalidFormat"; } @@ -635,10 +648,6 @@ public class GenericBinder implements Binder { public Alert getAlert() { return new AbstractAlert() { - public String getElement() { - return getProperty(); - } - public String getCode() { SpelMessage spelCode = ((SpelEvaluationException) cause).getMessageCode(); if (spelCode == SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE) { @@ -726,10 +735,6 @@ public class GenericBinder implements Binder { public Alert getAlert() { return new AbstractAlert() { - public String getElement() { - return getProperty(); - } - public String getCode() { return "bindSuccess"; } @@ -753,7 +758,7 @@ public class GenericBinder implements Binder { static abstract class AbstractAlert implements Alert { public String toString() { - return getElement() + ":" + getCode() + " - " + getMessage(); + return getCode() + " - " + getMessage(); } } } diff --git a/org.springframework.context/src/main/java/org/springframework/ui/lifecycle/BindAndValidateLifecycle.java b/org.springframework.context/src/main/java/org/springframework/ui/lifecycle/BindAndValidateLifecycle.java index d738303c302..dc766ce0050 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/lifecycle/BindAndValidateLifecycle.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/lifecycle/BindAndValidateLifecycle.java @@ -50,7 +50,7 @@ public class BindAndValidateLifecycle { validator.validate(binder.getModel(), bindingResults.successes().properties()); } for (BindingResult result : bindingResults.failures()) { - alertContext.add(result.getAlert()); + alertContext.add(result.getProperty(), result.getAlert()); } // TODO translate validation results into messages }