2010-02-28 12 views
8

Ho un metodo nel mio bean gestito che restituisce javascript come stringa. Quando il metodo viene richiamato dal tag head, funziona correttamente. Ma quando viene invocato da body, il browser invece di eseguire il javascript lo scrive così com'è. Quale può essere il problema?Come posso restituire l'HTML dal bean gestito in JSF?

Nella mia pagina JSF quando faccio #{IndexBean.EastRegionGadgets} in head funziona bene ma non in body. Emette l'HTML così com'è. Ecco il codice:

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; 

    } 


} 

risposta

18

Nella mia pagina JSF quando faccio #{IndexBean.EastRegionGadgets} in head funziona bene, ma non è così in body. Emette l'HTML così com'è.

suppongo che si sta utilizzando in <h:outputText>body per l'uscita del HTML. Come per lo documentation, per impostazione predefinita sfugge all'HTML. È necessario impostare l'attributo escape su false.

<h:outputText value="#{bean.html}" escape="false" /> 
+0

No in realtà non stavo usando nulla. Ho appena scritto # {bean.html} nel corpo e ho scritto tutto in HTML. Tuttavia, la stessa linea ha funzionato bene nel tag head! – TCM

+0

Ah sì, stai usando Facelets. – BalusC

+0

Sì, sto usando Facelets. Devo usare obbligatoriamente questo outputText per produrre html? dal fagiolo gestito? – TCM

Problemi correlati