8

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.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
chardex
  • 323
  • 1
  • 5
  • 17

3 Answers3

6

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.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
5

How about

<f:view contentType="text/html" />
Vivien Barousse
  • 19,611
  • 2
  • 55
  • 64
  • This solution is necessary to work with older versions of iPhone and iPad. Seems like later browers are over-ridding the default Content-Type and doing their best guess. But older browser were respecting the Content-Type header and expecting header. Great solution that was simple. – JeffJak Dec 13 '12 at 22:38
3

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>
Dejell
  • 12,840
  • 37
  • 129
  • 217
  • Isn't a `PhaseListener` a bit too overcomplicated "solution" for this simple issue? Regardless of this, why not just a `Filter` since you won't have any interest in the `FacesContext`? – BalusC Sep 27 '10 at 12:55
  • 1. it works in all the browser 2. He may need it in the future – Dejell Sep 27 '10 at 12:59
  • 1. I am not sure how doing the job inside a `PhaseListener` instead of a `Filter` or just straight in the view is been detected by the webbrowser. Can you elaborate? 2. Maybe. – BalusC Sep 27 '10 at 13:05
  • From my experience - when I had to add No-Cache to Header in this way it worked in all the browsers. using f:view or other tricks didn't do it. – Dejell Sep 27 '10 at 13:15
  • That's correct for *particularly* the cache headers. But that concerns a different problem. Hints in [this answer](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers/2068407#2068407). – BalusC Sep 27 '10 at 13:18
  • I think you misunderstood the hints and/or core-problem-cause: HTTP response headers have precedence over HTML meta headers. When not explicitly set, the webserver will supply some default HTTP response headers which may cause that HTML meta headers on same name don't have any effect. – BalusC Sep 27 '10 at 13:48