【问题标题】:How do you check the browser's user agent in a JSP page using JSTL, EL?如何使用 JSTL、EL 在 JSP 页面中检查浏览器的用户代理?
【发布时间】:2010-11-01 09:54:48
【问题描述】:

我需要检查浏览器的用户代理是否是 IE6。但是我不应该使用 scriptlet(我们有严格的 no scriptlets 政策)来做到这一点。

目前我使用

<%
String ua = request.getHeader( "User-Agent" );
boolean isMSIE = ( ua != null && ua.indexOf( "MSIE" ) != -1 );
%>

<% if( isMSIE ){ %>
<div>
<% } %>

使用 JSTL、EL 等而不是 scriptlet 最简洁的方法是什么?

【问题讨论】:

    标签: java jsp browser cross-browser user-agent


    【解决方案1】:
    <c:set var="browser" value="${header['User-Agent']}" scope="session"/>
    

    【讨论】:

      【解决方案2】:
      <c:if test="${fn:contains(header['User-Agent'],'MSIE')}"></c:if>
      

      【讨论】:

      • Zoltan,你需要在代码的开头添加四个空格才能正常显示。
      【解决方案3】:

      如果您使用spring-mobile 框架,您可以使用以下方法检查设备类型

      <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
          <c:choose> 
              <c:when test="${currentDevice.normal}"><p>"Welcome desktop user"</p> </c:when>
              <c:when test="${currentDevice.mobile}"><p>"Welcome mobile user"</p>  </c:when>
              <c:when test="${currentDevice.tab}"><p>"Welcome tab user"</p> </c:when>
          </c:choose>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 2012-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-19
        • 2016-07-10
        相关资源
        最近更新 更多