alerts static factory
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
* <pre>
|
||||
* 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");
|
||||
* }
|
||||
* </pre>
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -54,8 +54,8 @@ public class DefaultAlertContext implements AlertContext {
|
||||
return Collections.unmodifiableList(messages);
|
||||
}
|
||||
|
||||
public void add(Alert alert) {
|
||||
List<Alert> alerts = this.alerts.get(alert.getElement());
|
||||
public void add(String element, Alert alert) {
|
||||
List<Alert> alerts = this.alerts.get(element);
|
||||
alerts.add(alert);
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user