JSF,覆盖HTTP标头

时间:2010-09-27 12:42:46

标签: java jsf jsf-2 facelets

我强烈需要覆盖JSF 2.0 Content-Type 标头。默认为

Content-Type:application/xhtml+xml; charset=UTF-8

但我需要

Content-Type:text/html; charset=UTF-8

感谢。

3 个答案:

答案 0 :(得分:6)

使用right doctype

<!DOCTYPE html>

没有更多。也不要将<?xml?>声明置于顶部。这是最低限度的模板:

<!DOCTYPE html>
<html 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Insert your title</title>
    </h:head>
    <h:body>
        <h1>Hello World</h1>
    </h:body>
</html>

这是HTML5 doctype。它与XHTML 1.x标记完全兼容,并增加了更多优势。

答案 1 :(得分:5)

怎么样

<f:view contentType="text/html" />

答案 2 :(得分:3)

以下方法适用于所有浏览器:

写一个PhaseListener:

  public class ContentTypePhaseListener implements PhaseListener {


        public PhaseId getPhaseId()
        {
            return PhaseId.RENDER_RESPONSE;
        }

        public void afterPhase(PhaseEvent event)
        {
        }

        public void beforePhase(PhaseEvent event)
        {
            FacesContext facesContext = event.getFacesContext();
            HttpServletResponse response = (HttpServletResponse) facesContext
                    .getExternalContext().getResponse();
            response.addHeader("Content-Type", "text/html; charset=UTF-8");

        }
    }

并在faces-context.xml中注册:

<lifecycle>
        <phase-listener>com.mycompnay.listener.ContentTypePhaseListener </phase-listener>
    </lifecycle>