Wednesday, June 17, 2009

JSF 2.0 and dojo create NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7 on Safari

When testing our dojo facelets examples in Safari I realized that they don't work with Mojarra 2.0. After some investigation it turned out that Mojarra 2.0 sends a reponse-header Content-Type: application/xhtml+xml thus triggering a bug in Safari.With JSF 1.2 (also with Facelets) it was always Content-Type: text/html. The result is that Safari throws NO_MODIFICATION_ALLOWED_ERR on some DOM operations that are needed from the dojo side.

I added a Listener to set the Content-Type to text/html and - helas! - it works just fine.

public void beforePhase(PhaseEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
Object response = extContext.getResponse();
if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
String contentType = "text/html; charset=UTF-8";
if (response instanceof ServletResponse) {
((ServletResponse) response).setContentType(contentType);
} else if (response instanceof RenderResponse) {
((RenderResponse) response).setContentType(contentType);
}
}
}

10 comments:

  1. Thanks! Exactly what I was looking for, amazing that Safari can be such a pain to work with in this regard.

    ReplyDelete
  2. hey! although this post is a bit old now, i got the same problem - chrome throwing that JS-error

    i'm working with jsf not long enought to know where to put the listenerobject and how to set the attributes that call this method. can you give me a hint?

    ReplyDelete
  3. firestorm696, configure the listener in your faces-config.xml:

    <lifecycle>
    <phase-listener>org.j4fry.dojo.listener.SetContentTypeListener</phase-listener>
    </lifecycle>

    Best regards,
    Ganesh

    ReplyDelete
  4. I kiss your feet, master!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This article is over a year old but it very quickly resolved my cross-browser issues with a complex facelets based application. I need to understand the issue and the solution better but it certainly gets me up and running for now. Many, many thanks :)

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This was very helful :)
    We can also use contentType = " text/html " in < f : view > (spaced out cause blogspot would refuse to accept it.

    ReplyDelete