Java Code Examples for javax.faces.context.FacesContext#getMessages()

The following examples show how to use javax.faces.context.FacesContext#getMessages() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ee8-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 2
Source Project: ee8-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 3
/**
 * Save current messages for later restoration.
 * 
 * @param context
 *        The current faces context.
 */
public static void saveMessages(FacesContext context)
{
	if (context == null) return;

	// look in the session
	HttpSession s = (HttpSession) context.getExternalContext().getSession(false);
	if (s == null) return;

	// collect the messages from the context for restoration on the next rendering
	List msgs = new Vector();
	for (Iterator iMessages = context.getMessages(); iMessages.hasNext();)
	{
		FacesMessage msg = (FacesMessage) iMessages.next();
		msgs.add(msg);
	}

	// store the messages for this mode to find
	s.setAttribute(ATTR_MSGS, msgs);
}
 
Example 4
/**
 * Save current messages for later restoration.
 * 
 * @param context
 *        The current faces context.
 */
public static void saveMessages(FacesContext context)
{
	if (context == null) return;

	// look in the session
	HttpSession s = (HttpSession) context.getExternalContext().getSession(false);
	if (s == null) return;

	// collect the messages from the context for restoration on the next rendering
	List msgs = new Vector();
	for (Iterator iMessages = context.getMessages(); iMessages.hasNext();)
	{
		FacesMessage msg = (FacesMessage) iMessages.next();
		msgs.add(msg);
	}

	// store the messages for this mode to find
	s.setAttribute(ATTR_MSGS, msgs);
}
 
Example 5
Source Project: ee8-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 6
Source Project: ee8-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 7
Source Project: ee8-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 8
Source Project: ee8-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 9
Source Project: ee8-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 10
Source Project: ee7-sandbox   File: MessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Remove the messages that are not associated with any particular component
 * from the faces context and store them to the user's session.
 *
 * @return the number of removed messages.
 */
private int saveMessages(FacesContext facesContext) {
	// remove messages from the context
	List<FacesMessage> messages = new ArrayList<FacesMessage>();
	for(Iterator<FacesMessage> i = facesContext.getMessages(null); i.hasNext(); ) {
		messages.add(i.next());
		i.remove();
	}
	// store them in the session
	if(messages.size() == 0) {
		return 0;
	}
	Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
	// if there already are messages
	@SuppressWarnings("unchecked")
	List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
	if(existingMessages != null) {
		existingMessages.addAll(messages);
	}
	else {
		sessionMap.put(sessionToken, messages); // if these are the first messages
	}

	return messages.size();
}
 
Example 11
Source Project: development   File: JSFUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean replaceMessageInListIfExisting(String oldKey,
        Object[] oldParams, String newKey, Object[] newParams) {

    FacesContext fc = FacesContext.getCurrentInstance();
    String oldText = JSFUtils.getText(oldKey, oldParams);
    String newText = JSFUtils.getText(newKey, newParams);

    for (Iterator<FacesMessage> i = fc.getMessages(); i.hasNext();) {
        FacesMessage fm = i.next();
        if (fm.getDetail().equals(oldText)) {
            fm.setDetail(newText);
            fm.setSummary(newText);
            return true;
        }
    }
    return false;
}
 
Example 12
Source Project: library   File: FacesUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Clear the messages component on the user screen
 *
 * @param context the view context to be used
 */
public static void clearMessages(FacesContext context) {

    final Iterator<FacesMessage> messages = context.getMessages();

    while (messages.hasNext()) {
        messages.next();
        messages.remove();
    }
}
 
Example 13
/**
 * Clear the messages component on the user screen
 *
 * @param context the view context to be used
 */
public static void clearMessages(FacesContext context) {

    final Iterator<FacesMessage> messages = context.getMessages();

    while (messages.hasNext()) {
        messages.next();
        messages.remove();
    }
}
 
Example 14
/**
 * Clear the messages component on the user screen
 *
 * @param context the view context to be used
 */
public static void clearMessages(FacesContext context) {

    final Iterator<FacesMessage> messages = context.getMessages();

    while (messages.hasNext()) {
        messages.next();
        messages.remove();
    }
}
 
Example 15
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
	// Note our complete disregard for class cast and null exceptions....
	UIMessage uiMessage = (UIMessage)component;
	String clientId = uiMessage.findComponent(uiMessage.getFor()).getClientId(context);

	Iterator iter = context.getMessages(clientId);
	if (iter.hasNext()) {
		// Just do the first one.
		FacesMessage message = (FacesMessage)iter.next();
		renderMessage(context, component, message);
	}
}
 
Example 16
Source Project: BootsFaces-OSP   File: GrowlRenderer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
    if (!component.isRendered()){
        return;
    }
    
    Growl uiGrowl = (Growl) component;
    ResponseWriter writer = facesContext.getResponseWriter();
    
    String clientId = uiGrowl.getClientId(facesContext);
    
    // get the for value
    String forValue = uiGrowl.getFor();
   
    Iterator<FacesMessage> allMessages = null;
    if ( uiGrowl.isGlobalOnly() ) {
    	allMessages = facesContext.getMessages(null); 
    } else if(forValue != null && forValue.length() > 0) {
    	forValue = ExpressionResolver.getComponentIDs(facesContext, uiGrowl, forValue);
    	allMessages = facesContext.getMessages(forValue);
    } else {
    	allMessages = facesContext.getMessages();
    }
    
    writer.startElement("script", uiGrowl);
    writer.writeAttribute("id", clientId, "id");
    writer.writeText("$(function() { ", null);
    
    while (allMessages.hasNext()) {
        FacesMessage message = allMessages.next();
        if (!shouldBeRendered(message, uiGrowl)){
            continue;
        }
        
        encodeSeverityMessage(facesContext, uiGrowl, message);
        message.rendered();
    }
    writer.writeText("});", null);
    writer.endElement("script");
}
 
Example 17
Source Project: development   File: JSFUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean hasWarnings(FacesContext fc) {
    for (Iterator<FacesMessage> i = fc.getMessages(); i.hasNext();) {
        FacesMessage m = i.next();
        if (FacesMessage.SEVERITY_WARN == m.getSeverity()) {
            return true;
        }
    }
    return false;
}
 
Example 18
private boolean containsMessageInList(FacesContext fc, String msg) {
    for (Iterator<FacesMessage> i = fc.getMessages(); i.hasNext();) {
        if (i.next().getDetail().contains(msg)) {
            return true;
        }
    }
    return false;
}
 
Example 19
@SuppressWarnings("unchecked")//$NON-NLS-1$
protected static Iterator<FacesMessage> getMessages(FacesContext context, String clientId) {
    return context.getMessages(clientId);
}
 
Example 20
@SuppressWarnings("unchecked")//$NON-NLS-1$
protected static Iterator<FacesMessage> getMessages(FacesContext context) {
    return context.getMessages();
}