2

I have a property in my backing bean that returns html code:

public String getHtmlPrevisualizar() {
    return "<html><head><title></title></head><body>Hello world.</body></html>";
}

What I want to do is show this html code in a iframe. I do this with javascript. This is the xhtml page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <f:loadBundle basename="resources" var="msg" />
<head>
    <title>#{msg['pageTitle']}</title>
</head>
<body>
<ui:composition template="/WEB-INF/facelets/templates/sqa/plantilla.xhtml">  
    <ui:define name="title">#{msg['pageTitle']}</ui:define>
    <ui:define name="javascript">
        <script type="text/javascript">
            function showPreview() {
                var doc = document.getElementById('iframePreview').contentWindow.document;
                doc.open();
                doc.write('#{nuevoEditarEstructura.htmlPrevisualizar}');
                doc.close();
                return false;
            }
            function showPreview2() {
                var doc = document.getElementById('iframePreview').contentWindow.document;
                doc.open();
                doc.write('<html><head><title></title></head><body>Hello world.</body></html>');
                doc.close();
                return false;
            }
        </script>
    </ui:define>
    <ui:define name="content">
        <h:form>
            <a4j:commandLink value="Preview" styleClass="boton" onclick="showPreview();"/>
            <a4j:commandLink value="Preview2" styleClass="boton" onclick="showPreview2();"/>
            <br/>
            <br/>
            <h:outputText value="#{nuevoEditarEstructura.htmlPrevisualizar}" />
            <br/>
            <br/>
            #{nuevoEditarEstructura.htmlPrevisualizar}
            <br/>
            <br/>
        </h:form>
        <iframe id="iframePreview">
        </iframe>
    </ui:define>  
</ui:composition>
</body>
</html>

There are two commandLinks. The first one gets the html code from the backing bean, the second one has the html code written in a string in javascript. The first commandLink doesn't work. If I view the source code of the page, the value thas should have returned from the backing bean is empty.

I have printed the value from the property in the backing bean also with this:

        <h:outputText value="#{nuevoEditarEstructura.htmlPrevisualizar}" />
        <br/>
        <br/>
        #{nuevoEditarEstructura.htmlPrevisualizar}

But nothig is shown. I have called getHtmlPrevisualizar() and printed its content in the eclipse console, and it returns the right html code.

I know that there can be some problems with escaped characters and facelets, I was expecting to have to deal with the characters in the html being escaped, but I don't get anything.

2 Answers 2

3

Set escape="false" in your <h:outputText /> tag component

<h:outputText value="#{nuevoEditarEstructura.htmlPrevisualizar}" escape="false" />
Sign up to request clarification or add additional context in comments.

4 Comments

I'll try that tomorrow at work, but why would that fix the problem? I'm not getting the string escaped, I'm just not getting any string at all.
If the last doesn't work, try to create a String with that value and use its getter, that should do it.
Finally, the problem was a silly one: the name of the backing bean was misspelled. Thanks for your help.
Don't worry, it happens to everybody when we have too much stress or keep working in one single thing with no rest.
0

Well, this is embarrassing. The problem was that the backing bean name was misspelled, nothing else. I wish that we could get some kind of warning for this, instead of just silently fail.

1 Comment

@LuiggiMendoza Well, this is the actual answer to the question, that's why I wrote an answer and not a comment. I thought about deleting the question, but perhaps if somebody else has the problem that a call to the backig bean retuns nothing, this question could help them check that the backing bean name is correctly spelled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.