The <h:outputText> is used in JSF to display plain text
(depending on how is used, it produces a <span> tag or no extra HTML markup)
Common/basic
usage in JSF:
<?xml
version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html">
<h:head>
<title>JSF outputText examples</title>
</h:head>
<h:body>
<h:outputText value="#{playerBean.name}"/>
<!-- in this case, you can simply use
#{playerBean.name} -->
</h:body>
</html>
The <h:outputText>
will be rendered in HTML as (there is no HTML markup):
Rafael Nadal
The PlayerBean
will be:
package
beans;
import
java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import
javax.inject.Named;
@Named
@SessionScoped
public class
PlayerBean implements Serializable{
private String name="Rafael Nadal";
private String coolname="<font
face=\"verdana\" color=\"green\">Rafael
Nadal</font>";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCoolname() {
return
coolname;
}
public void setCoolname(String coolname) {
this.coolname = coolname;
}
}
Data flow in
image:
More examples:
Basic/common usage (the presence of id produce <span> markup)
<h:outputText
id="nameId" value="#{playerBean.name}"/>
<!-- this
will render <span id="nameId">Rafael Nadal</span> -->
Styling text with styleClass (the presence of styleClass produce <span> markup)
<h:outputText value="#{playerBean.name}"
styleClass="my_class"/>
<!-- this
will render <span class="ot">Rafael Nadal</span> -->
Styling text with style (the presence of style produce <span> markup)
<h:outputText
value="#{playerBean.name}" style="color:green;"/>
<!-- this
will render <span style="color:green;">Rafael
Nadal</span> -->
Indicate text direction dir (the presence of dir produce <span> markup)
<h:outputText
lang="LTR" value="#{playerBean.name}"/>
<!-- this
will render <span dir="RTL">Rafael Nadal</span> -->
Escape HTML
<h:outputText
escape="true" value="#{playerBean.coolname}"/>
<!-- this
will render <font face="verdana"
color="green">Rafael Nadal</font> -->
No escape HTML
<h:outputText
escape="false" value="#{playerBean.coolname}"/>
<!-- this
will render <font face="verdana" color="green">Rafael
Nadal</font> -->
Note If any of attributes id, styleClass, style, dir, lang, title is present or pass-through attributes are present, the <h:outputText> will be rendered as HTML, <span> tag. Otherwise it will not produce HTML markup.
Note Pay
attention when you are using the escape attribute set to false.
The XSS attacks are implicitly prevented by JSF through the escape
attribute, which is set to true by default.
Complete source code on GitHub.
See also Mkyong.com.
More resources on Constantin Alin, ZEEF page.
OutputText in JSF Extension on JSF ShowCase ZEEF page.
More resources on Constantin Alin, ZEEF page.
OutputText in JSF Extension on JSF ShowCase ZEEF page.
Niciun comentariu :
Trimiteți un comentariu