我的托管bean中有一个方法,它将javascript作为字符串返回。从head
标记调用该方法时,它可以正常工作。但是当它从body
调用时,浏览器而不是渲染javascript会按原样写入。可能是什么问题?
当我在#{IndexBean.EastRegionGadgets}
中head
时,我的JSF页面工作正常,但不在body
中。它按原样输出HTML。这是代码:
package BusinessFacade;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlOutputText;
enum REGION{
NORTH,EAST,WEST;
}
class Gadget{
private String gadgetCode = "";
private REGION gadgetRegion = REGION.WEST;
public Gadget(String gadgetCode, REGION gadgetRegion){
this.gadgetCode = gadgetCode;
this.gadgetRegion = gadgetRegion;
}
public String getGadgetCode() {
return gadgetCode;
}
public void setGadgetCode(String gadgetCode) {
this.gadgetCode = gadgetCode;
}
public REGION getGadgetRegion() {
return gadgetRegion;
}
public void setGadgetRegion(REGION gadgetRegion) {
this.gadgetRegion = gadgetRegion;
}
}
@ManagedBean(name="IndexBean")
@RequestScoped
public class IndexBean {
ArrayList<Gadget> _list;
public IndexBean() {
}
@PostConstruct
public void initialize(){
_list = new ArrayList<Gadget>();
Gadget objGadget = new Gadget("<script type='text/javascript' src='http://cdn.widgetserver.com/syndication/subscriber/InsertWidget.js'></script><script>if (WIDGETBOX) WIDGETBOX.renderWidget('78d12c15-dc87-42f2-a78a-3f62a91a119a');</script><noscript>Get the <a href='http://www.widgetbox.com/widget/crystal-clock'>Crystal Clock</a> widget and many other <a href='http://www.widgetbox.com/'>great free widgets</a> at <a href='http://www.widgetbox.com'>Widgetbox</a>! Not seeing a widget? (<a href='http://docs.widgetbox.com/using-widgets/installing-widgets/why-cant-i-see-my-widget/'>More info</a>)</noscript>",REGION.WEST);
_list.add(objGadget);
objGadget = new Gadget("<script type='text/javascript' src='http://cdn.widgetserver.com/syndication/subscriber/InsertWidget.js'></script><script>if (WIDGETBOX) WIDGETBOX.renderWidget('1ccc3dee-8266-4b84-8191-13a4bf584d0c');</script><noscript>Get the <a href='http://www.widgetbox.com/widget/custom-clock'>Shiny Clock</a> widget and many other <a href='http://www.widgetbox.com/'>great free widgets</a> at <a href='http://www.widgetbox.com'>Widgetbox</a>! Not seeing a widget? (<a href='http://docs.widgetbox.com/using-widgets/installing-widgets/why-cant-i-see-my-widget/'>More info</a>)</noscript>",REGION.EAST);
_list.add(objGadget);
}
public String getWestRegionGadgets(){
HtmlOutputText objHtmlOutputText = new HtmlOutputText();
String strGadgets = "";
for(Gadget objGadget:_list ){
if(objGadget.getGadgetRegion() == REGION.WEST){
strGadgets += objGadget.getGadgetCode();
}
}
return strGadgets;
}
public String getEastRegionGadgets(){
String strGadgets = "";
for(Gadget objGadget:_list ){
if(objGadget.getGadgetRegion() == REGION.EAST){
strGadgets += objGadget.getGadgetCode();
}
}
return strGadgets;
}
}
答案 0 :(得分:19)
在我的JSF页面中,当
#{IndexBean.EastRegionGadgets}
head
body
时,它工作正常,但不在<h:outputText>
中。它按原样输出HTML。
我想你在body
中使用escape
来输出HTML。根据{{3}},默认情况下它会转义HTML。您需要将其false
属性设置为<h:outputText value="#{bean.html}" escape="false" />
。
{{1}}