kratos2333
1/13/2017 - 9:03 AM

Add message

Add message

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="/templates/masterLayout.xhtml">
    <ui:define name="chapterItem">
        <h3 style="margin-top:0">Internationalization of the Faces Messages</h3>

        <p:growl id="growl" />
        <p:commandButton action="#{localizationBean.addMessage}" value="Display Message" update="growl" />
    </ui:define>
</ui:composition>
package org.primefaces.cookbook.utils;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.MessageFormat;
import java.util.ResourceBundle;

/**
 * MessageUtil
 *
 * @author Oleg Varaksin / last modified by $Author: $
 * @version $Revision: 1.0 $
 */
public class MessageUtil {

    public static void addInfoMessage(String str) {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msg");
        String message = bundle.getString(str);
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, ""));
    }

    public static void addInfoMessage(String summary, String detail) {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msg");
        String message = bundle.getString(summary);
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, detail));
    }

    public static void addInfoMessage(String str, Object... args) {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msg");
        String message = bundle.getString(str);
        if (args != null) {
            message = MessageFormat.format(message, args);
        }
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, ""));
    }

    public static void addInfoMessageWithoutKey(String summary, String detail) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail));
    }

    public static String getMessage(String key, Object... args) {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msg");
        String message = bundle.getString(key);
        if (args != null) {
            message = MessageFormat.format(message, args);
        }

        return message;
    }
}
package org.primefaces.cookbook.controller.chapter1;

import org.primefaces.cookbook.utils.MessageUtil;

import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;

/**
 * User: mertcaliskan
 * Date: 6/21/12
 */
@Named
@ViewScoped
public class LocalizationBean implements Serializable {

	private boolean selectedValue = true;

	public boolean isSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(boolean selectedValue) {
		this.selectedValue = selectedValue;
	}

	public String addMessage() {
		MessageUtil.addInfoMessage("broadcast.message");

		return null;
	}
}