myfaces htmlHead 未首先呈现
myfaces htmlHead is not rendered first
我已经开始维护遗留的 JSF 应用程序。它使用 myFaces 1.1.x 和 tomahawk 1.1.6。我需要打印 META
标签 X-UA-Compatible
作为 HEAD
元素的第一个元素,否则 IE 将忽略它。
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="t" uri="http://myfaces.apache.org/tomahawk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<f:view>
<t:documentHead>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
会生成
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/JSCookMenu.js"><!--
//--></script>
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/MyFacesHack.js"><!--
//--></script>
<script type="text/javascript"><!--
var myThemeMiniBlackBase='/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/ThemeMiniBlack/';
//--></script>
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/ThemeMiniBlack/theme.js"><!--
//--></script>
<link rel="stylesheet" href="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/ThemeMiniBlack/theme.css" type="text/css" />
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/popup.HtmlPopupRenderer/JSPopup.js"><!--
//--></script>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
如何强制 META 标签成为第一个元素?我尝试了普通的 HTML HEAD 元素,但它也没有用。
我需要
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
更新:
其中一些 javascripts 和样式是由于此标记而呈现的:
<t:jscookMenu theme="ThemeMiniBlack" layout="hbr" immediate="true">
剩下的javascript来自
<t:popup closePopupOnExitingElement="true" closePopupOnExitingPopup="true"
displayAtDistanceX="10" displayAtDistanceY="20" styleClass="popup">
问题是我是否可以强制我放入 t:documentHead 的内容具有优先权?我担心我需要编写自定义标签。
我会尝试在自定义 java 过滤器中自己添加元标记。
<filter>
<filter-name>MetaTagsFilter</filter-name>
<filter-class>com.yournamespace.MetaTagsFilter</filter-class>
</filter>
然后为 JSF servlet 做一个过滤器映射:
<filter-mapping>
<filter-name>MetaTagsFilter</filter-name>
<servlet-name>FacesServlet</servlet-name>
</filter-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
在您的过滤器逻辑中,添加元标记,在 servlet 执行处理后,类似这样:
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HtmlResponseWrapper capturingResponseWrapper = new HtmlResponseWrapper(
(HttpServletResponse) response);
filterChain.doFilter(request, capturingResponseWrapper);
if (response.getContentType() != null
&& response.getContentType().contains("text/html")) {
// Try this
capturingResponseWrapper.addHeader("X-UA-Compatible", "IE=edge");
// Or this, that way you have more control over WHERE it gets written
String content = capturingResponseWrapper.getCaptureAsString();
// replace stuff here (find the <head></head> tags and add the <meta> at the very beginning)
System.out.println(replacedContent);
response.getWriter().write(replacedContent);
}
}
另请参阅:
- Modify html response using filter
- Is doFilter() executed before or after the Servlet's work is done?
- What is the difference between creating JSF pages with .jsp or .xhtml or .jsf extension
- The Essentials of Filters (Oracle)
我已经开始维护遗留的 JSF 应用程序。它使用 myFaces 1.1.x 和 tomahawk 1.1.6。我需要打印 META
标签 X-UA-Compatible
作为 HEAD
元素的第一个元素,否则 IE 将忽略它。
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="t" uri="http://myfaces.apache.org/tomahawk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<f:view>
<t:documentHead>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
会生成
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/JSCookMenu.js"><!--
//--></script>
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/MyFacesHack.js"><!--
//--></script>
<script type="text/javascript"><!--
var myThemeMiniBlackBase='/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/ThemeMiniBlack/';
//--></script>
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/ThemeMiniBlack/theme.js"><!--
//--></script>
<link rel="stylesheet" href="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/navmenu.jscookmenu.HtmlJSCookMenuRenderer/ThemeMiniBlack/theme.css" type="text/css" />
<script type="text/javascript" src="/Viewer/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/15311471/popup.HtmlPopupRenderer/JSPopup.js"><!--
//--></script>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
如何强制 META 标签成为第一个元素?我尝试了普通的 HTML HEAD 元素,但它也没有用。
我需要
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
更新:
其中一些 javascripts 和样式是由于此标记而呈现的:
<t:jscookMenu theme="ThemeMiniBlack" layout="hbr" immediate="true">
剩下的javascript来自
<t:popup closePopupOnExitingElement="true" closePopupOnExitingPopup="true"
displayAtDistanceX="10" displayAtDistanceY="20" styleClass="popup">
问题是我是否可以强制我放入 t:documentHead 的内容具有优先权?我担心我需要编写自定义标签。
我会尝试在自定义 java 过滤器中自己添加元标记。
<filter>
<filter-name>MetaTagsFilter</filter-name>
<filter-class>com.yournamespace.MetaTagsFilter</filter-class>
</filter>
然后为 JSF servlet 做一个过滤器映射:
<filter-mapping>
<filter-name>MetaTagsFilter</filter-name>
<servlet-name>FacesServlet</servlet-name>
</filter-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
在您的过滤器逻辑中,添加元标记,在 servlet 执行处理后,类似这样:
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HtmlResponseWrapper capturingResponseWrapper = new HtmlResponseWrapper(
(HttpServletResponse) response);
filterChain.doFilter(request, capturingResponseWrapper);
if (response.getContentType() != null
&& response.getContentType().contains("text/html")) {
// Try this
capturingResponseWrapper.addHeader("X-UA-Compatible", "IE=edge");
// Or this, that way you have more control over WHERE it gets written
String content = capturingResponseWrapper.getCaptureAsString();
// replace stuff here (find the <head></head> tags and add the <meta> at the very beginning)
System.out.println(replacedContent);
response.getWriter().write(replacedContent);
}
}
另请参阅:
- Modify html response using filter
- Is doFilter() executed before or after the Servlet's work is done?
- What is the difference between creating JSF pages with .jsp or .xhtml or .jsf extension
- The Essentials of Filters (Oracle)