问题
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.
回答1:
Set escape="false"
in your <h:outputText />
tag component
<h:outputText value="#{nuevoEditarEstructura.htmlPrevisualizar}" escape="false" />
回答2:
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.
来源:https://stackoverflow.com/questions/11020438/backing-bean-property-that-should-return-a-string-with-html-code-returns-empty-s