Phase listener to set IE to edge or X-UA-Compatible value

//java placed in a java design element


package com.refchem.app;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletResponse;

public class UACompatibleHeaderPhaseListener implements PhaseListener {
	private static final long serialVersionUID = 1L;

	// @Override
	public PhaseId getPhaseId() {
		return PhaseId.RENDER_RESPONSE;
	}

	// @Override
	public void beforePhase(PhaseEvent event) {
		try {
			final FacesContext facesContext = event.getFacesContext();
			final HttpServletResponse response = (HttpServletResponse) facesContext
					.getExternalContext().getResponse();
			response.addHeader("X-UA-Compatible", "IE=edge");
		} catch (Exception e) {
			System.out.println("err setting header");
		}
	}

	// @Override
	public void afterPhase(PhaseEvent event) {

	}

}







//this is put in the Faces config
<lifecycle>
   <phase-listener>com.refchem.app.UACompatibleHeaderPhaseListener</phase-listener>
 </lifecycle>

Java
mark t hughes
November 12, 2012 7:40 AM
Rating
424

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



2 comment(s)Login first to comment...
Steve Cochrane
(at 09:37 on 21.04.2015)
Why not add the tag to the theme?...

{metaData}
{httpEquiv}X-UA-Compatible{/httpEquiv}
{content}IE=Edge{/content}
{/metaData}
P Solano
(at 07:59 on 17.11.2012)
response.addHeader("X-UA-Compatible", "IE=edge") is not working for me; I had to change it to:
response.setHeader("X-UA-Compatible", "IE=edge");

I'm still running 8.5.2 so I had to add this code under my own Source Folder and added it to Build Path from Package Explorer.
Thanks for this tip.