开发者

How to display Browser name and version with JSF 2.0?

I just need to display the browser name and its version on a <h:outpu开发者_JAVA百科tText/> in the user's homepage. Can we achieve this via JSF 2.0?


Mojarra 2.0.4 - Primefaces 2.2.1- glassfish v3


Put this method in your bean:

public String getBrowserName() {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String userAgent = externalContext.getRequestHeaderMap().get("User-Agent");

    if(userAgent.contains("MSIE")){ 
        return "Internet Explorer";
    }
    if(userAgent.contains("Firefox")){ 
        return "Firefox";
    }
    if(userAgent.contains("Chrome")){ 
        return "Chrome";
    }
    if(userAgent.contains("Opera")){ 
        return "Opera";
    }
    if(userAgent.contains("Safari")){ 
        return "Safari";
    }
    return "Unknown";
}

And then:

<h:outputText value="Browser: #{yourBean.browserName}" />


There are as far as I know no JSF components which does that with a single tag or something. The easiest what you can do is just displaying the raw HTTP User-Agent header.

<h:outputText value="#{header['user-agent']}" />

This is only a large and ugly string which is not always decipherable to everyone.

There are however APIs which can convert a HTTP User-Agent header into useable information, such as the exact browser make/version and platform make/version, such as useragentstring.com.

Once converted the User-Agent header into useable parts with help of such an API, you must be able to display the parts of interest in JSF with help of a managed bean the usual way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜