Skip to content

Commit

Permalink
Merge pull request #799 from caelum/ot-vmessageswithreqattr
Browse files Browse the repository at this point in the history
Adding Messages as request attribute to enforce flash scope when redirect
  • Loading branch information
Turini committed Sep 17, 2014
2 parents 378fca8 + b612555 commit 1dd7e4e
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;

Expand All @@ -40,15 +42,39 @@
* @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<Severity, List<Message>> 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())) {
Expand Down

0 comments on commit 1dd7e4e

Please sign in to comment.