prints HTML as-is instead of actual HTML [dupl

2019-02-06 01:04发布

问题:

This question already has an answer here:

  • Component to inject and interpret String with HTML code into JSF page 1 answer

I am using JSF 1.2

I am trying to print text using <h:outputtext>

<h:outputText id="warningDose" styleClass="redText" value="#{templatePrescriptionMaintenanceBackingBean.doseWarningText}"></h:outputText>

Now this variable contains text with html tags. <b>,<i> etc...

But that displays content as it is instead of actual bold or italic html output.

Is there any way we can make this <h:outputText> such that it gives html response?

回答1:

You should set in the h:outputText tag:

escape="false"

But remember that mixing "view" construction (i.e., creating a string with HTML tags) between the JSF view page and the underlying bean is kinda bad practice. All the "view production" should be in the view page.



回答2:

Just set it to not escape.

<h:outputText id="warningDose" escape="false" styleClass="redText" value="#{templatePrescriptionMaintenanceBackingBean.doseWarningText}"></h:outputText>


回答3:

I had a very similar problem. My question is here

My xhtml page looks like -

<h:outputText  itemEscaped="false" escape="false"    value="#{singleViewResultDO.associatedCode}" />

associatedCode is getting value from a SQL query where I want to use HTML tag to have conditional styling.

Here is my SQL query looks like:

Select A, REPLACE(Wm_Concat(DISTINCT  CASE WHEN sv.rmvd = 0 THEN  ' '||sv.CMPNION_CD  ELSE '<span style=\"color:red; \">' || ' '||sv.CMPNION_CD|| '</span>' END),' , ','') ,  "
from Table

Ignore REPLACE and WM_CONCAT as it is for displaying comma separated values. The piece of code relevant is

CASE WHEN sv.rmvd = 0 THEN  ' '||sv.CMPNION_CD  ELSE '<span style=\"color:red; \">' || ' '||sv.CMPNION_CD|| '</span>' END

I want to have tag based on a condition rmvd = 0.. Since, I have escape="false" in my , I don't need to escape my html tags in query. What I mean is no need to convert < to < > to > and " to "

Also note that since I have double quotes "" in my span, I need to escape it once so it won't be escaped when it reaches .

I am getting my output as desired - 8000778 in red color