Question

I strongly need to override JSF 2.0 Content-Type header. Default it is

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

But I need

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

Thanks.

Was it helpful?

Solution

How about

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

OTHER TIPS

Use the right doctype.

<!DOCTYPE html>

Nothing more. Also don't put <?xml?> declaration at top. Here's a minimum template:

<!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>

It's the HTML5 doctype. It's fully compatible with XHTML 1.x markup and adds more advantages.

The following approach works in all the browsers:

Write a 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");

        }
    }

and register it in faces-context.xml:

<lifecycle>
        <phase-listener>com.mycompnay.listener.ContentTypePhaseListener </phase-listener>
    </lifecycle>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top