生成跨度的HTML代码?

时间:2011-03-02 13:51:28

标签: java html jsf

我想为jsf页面生成一些html代码(我不是在jsp上所以我不能使用out.print),所以我使用icefaces组件ice:outputText:

<ice:outputText id="txtCaptcha" escape="false"></ice:outputText>

我通过Java代码

中的id成功找到了outputText
UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
HtmlOutputText icefacesOutputText = (HtmlOutputText) root
.findComponent("mainContentForm:txtCaptcha");

并在其中放入一些HTML代码,因此使用Firebug我可以查看:

<span class="iceOutTxt" id="mainContentForm:txtCaptcha" style="height:300px; width:100%;"><script type="text/javascript" src="http://api.recaptcha.net/challenge?k=<key>"></script>
<noscript>
    <iframe src="http://api.recaptcha.net/noscript?k=<key>" height="300" width="500" frameborder="0"></iframe><br>
    <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
    <input type="hidden" name="recaptcha_response_field" value="">
</noscript></span>

嗯......我不明白为什么它不会出现在html页面中?为什么我只能看到Firebug的内容?

提前致谢

1 个答案:

答案 0 :(得分:0)

这是另一个建议:

<ice:outputText id="txtCaptcha" value="#{myBean.someHTML}" escape="false"/>

然后在bean myBean的getter中,返回HTML代码:

public String getSomeHTML() {
    return "<span>...</span>";
}

在设置escape="false"属性时,它不会转义HTML字符,因此会生成它。