Java 怎么生成JSF错误消息

2019年8月13日22:14:11 评论 28

1.简介

Java 怎么生成JSF错误消息?——请看下文。

2.代码

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

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String summary = "tah1986.com";
        error(summary);
    }
    
    public static void error(String summary) {
        error(summary, null, null);
    }
    
    public static void error(String summary, String detail) {
        error(summary, detail, null);
    }
    
    public static void error(UIComponent component, String summary) {
        error(summary, null, component);
    }
    
    public static void error(String summary, String detail,
            UIComponent component) {
        message(summary, detail, component, FacesMessage.SEVERITY_ERROR);
    }
    
    public static void message(String summary, String detail,
            UIComponent component, Severity severity) {
        FacesMessage fm = new FacesMessage(severity, summary, detail);
        if (component == null) {
            FacesContext.getCurrentInstance().addMessage(null, fm);
        } else {
            FacesContext.getCurrentInstance().addMessage(component.getId(),
                    fm);
        }
    }
}

 

3.总结

示例代码演示了 Java 如何生成JSF错误消息,具体步骤参见示例代码。

发表评论

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