应该返回带有html代码的字符串的bean属性返回空字符串 [英] Backing bean property that should return a string with html code returns empty string

查看:99
本文介绍了应该返回带有html代码的字符串的bean属性返回空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public String getHtmlPrevisualizar(){
返回< 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\"> 
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>

< head>
< title>#{msg ['pageTitle']}< / title>
< / head>
< body>
< 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();
返回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();
返回false;
}
< / script>
< / ui:define>
< ui:define name =content>
< h:form>
< a4j:commandLink value =预览styleClass =botononclick =showPreview();/>
< a4j:commandLink value =Preview2styleClass =botononclick =showPreview2();/>
< br />
< br />
< h:outputText value =#{nuevoEditarEstructura.htmlPrevisualizar}/>
< br />
< br />
#{nuevoEditarEstructura.htmlPrevisualizar}
< br />
< br />
< / h:表格>
< iframe id =iframePreview>
< / iframe>
< / ui:define>
< / ui:composition>
< / body>
< / html>

有两个commandLinks。第一个从backing bean获取html代码,第二个在javascript中使用字符串编写html代码。第一个commandLink不起作用。如果我查看页面的源代码,则应该从backing bean返回的值为空。



我已经打印了支持bean中属性的值还有:

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

但显示nothig。我已经调用了 getHtmlPrevisualizar()并在eclipse控制台中打印了它的内容,并且它返回了正确的html代码。



我知道转义字符和facelets可能存在一些问题,我期望不得不处理html中被转义的字符,但我什么都没有。

解决方案

好吧,这很尴尬。问题是支持bean名称拼写错误,没有别的。我希望我们能够得到这样的警告,而不是一味地失败。


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.

解决方案

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.

这篇关于应该返回带有html代码的字符串的bean属性返回空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆