From 780bb917889bf88d8cef6e1bbb90be6f27d4f1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Tue, 16 Sep 2014 02:22:51 -0300 Subject: [PATCH 1/2] Adding Messages as request attribute to enforce flash scope when redirect --- .../br/com/caelum/vraptor/validator/DefaultValidator.java | 1 + .../main/java/br/com/caelum/vraptor/validator/Messages.java | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java index ab8af5aa3..c34ab11ff 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java @@ -140,6 +140,7 @@ public T onErrorUse(Class view) { } result.include("errors", getErrors()); + result.include(Messages.ATTRIBUTE_KEY, messages); outjector.outjectRequestMap(); logger.debug("there are errors on result: {}", getErrors()); diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java index 6172a96b3..a3f935b21 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java @@ -23,7 +23,6 @@ import java.util.Map; import javax.enterprise.context.RequestScoped; -import javax.inject.Named; import org.slf4j.Logger; @@ -40,10 +39,11 @@ * @since 4.1 * @author Otávio S Garcia */ -@Named("vmessages") @RequestScoped public class Messages { + public static final String ATTRIBUTE_KEY = "vmessages"; + private final static Logger log = getLogger(Messages.class); private Map> messages = new HashMap<>(); From b6125559b4c890177134fa2260e0c28bfdd095ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Wed, 17 Sep 2014 00:15:21 -0300 Subject: [PATCH 2/2] Adding messages to request attributes --- .../vraptor/validator/DefaultValidator.java | 1 - .../caelum/vraptor/validator/Messages.java | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java index c34ab11ff..ab8af5aa3 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/DefaultValidator.java @@ -140,7 +140,6 @@ public T onErrorUse(Class view) { } result.include("errors", getErrors()); - result.include(Messages.ATTRIBUTE_KEY, messages); outjector.outjectRequestMap(); logger.debug("there are errors on result: {}", getErrors()); diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java index a3f935b21..364097114 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/validator/Messages.java @@ -22,7 +22,10 @@ import java.util.List; import java.util.Map; +import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; @@ -49,6 +52,29 @@ public class Messages { private Map> messages = new HashMap<>(); private boolean unhandledErrors = false; + private final HttpServletRequest request; + + /** + * @deprecated CDI eyes only + */ + protected Messages() { + this(null); + } + + @Inject + public Messages(HttpServletRequest request) { + this.request = request; + } + + /** + * Put this instance into request attributes to allow this instance survive between requests using flash + * scope. + */ + @PostConstruct + public void init() { + request.setAttribute(ATTRIBUTE_KEY, this); + } + public Messages add(Message message) { get(message.getSeverity()).add(message); if(Severity.ERROR.equals(message.getSeverity())) {