【问题标题】:Backing bean property that should return a string with html code returns empty string应该返回带有 html 代码的字符串的支持 bean 属性返回空字符串
【发布时间】:2012-06-16 17:44:02
【问题描述】:

我的后备 bean 中有一个返回 html 代码的属性:

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

我想做的是在 iframe 中显示此 html 代码。我用javascript做这个。这是xhtml页面:

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

有两个命令链接。第一个从支持 bean 获取 html 代码,第二个将 html 代码写在 javascript 中的字符串中。第一个 commandLink 不起作用。如果我查看页面的源代码,应该从 backing bean 返回的值是空的。

我也打印了支持 bean 中属性的值:

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

但什么都没有显示。我已经调用了getHtmlPrevisualizar() 并在 Eclipse 控制台中打印了它的内容,它返回了正确的 html 代码。

我知道转义字符和 facelets 可能存在一些问题,我原以为必须处理 html 中被转义的字符,但我什么也没得到。

【问题讨论】:

    标签: java html jsf facelets jsf-1.2


    【解决方案1】:

    &lt;h:outputText /&gt; 标签组件中设置escape="false"

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

    【讨论】:

    • 我明天上班时试试,但为什么会解决问题呢?我没有得到字符串转义,我根本没有得到任何字符串。
    • 如果最后一个不起作用,请尝试使用该值创建一个 String 并使用它的 getter,应该可以做到。
    • 最后,问题很愚蠢:backing bean 的名称拼写错误。感谢您的帮助。
    • 别担心,当我们压力过大或不停地工作一件事时,每个人都会遇到这种情况。
    【解决方案2】:

    嗯,这很尴尬。问题是支持 bean 名称拼写错误,仅此而已。我希望我们能得到某种警告,而不是默默地失败。

    【讨论】:

    • @LuiggiMendoza 好吧,这是问题的实际答案,这就是我写答案而不是评论的原因。我考虑过删除这个问题,但也许如果其他人遇到调用 backig bean 没有返回任何问题的问题,这个问题可以帮助他们检查 backig bean 名称的拼写是否正确。
    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2018-09-10
    • 2020-01-27
    • 2020-07-11
    相关资源
    最近更新 更多