Java 怎么向JSF FacesContext添加错误消息

2019年8月13日22:05:01 评论 30

1.简介

Java 怎么向JSF FacesContext添加错误消息?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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

public class Main{
    public static void main(String[] argv) throws Exception{
        String key = "tah1986.com";
        addErrorMessage(key);
    }
    public static void addErrorMessage(String key) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.addMessage("", createErrorMessage(key));
    }
    public static FacesMessage createErrorMessage(String key) {
        return new FacesMessage(FacesMessage.SEVERITY_ERROR, "",
                getMessages(key));

    }
    public static String getMessages(String key) {
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceBundle bundle = ResourceBundle.getBundle(
                "com.formation.bundle.messagesErr", context
                        .getApplication().getDefaultLocale());
        String message = null;
        try {
            message = bundle.getString(key);
        } catch (Exception e) {
            return "unkown message " + key;
        }
        return message;
    }
}

 

3.总结

示例代码演示了 Java 怎么向JSF FacesContext添加错误消息,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: