Skip to content Skip to sidebar Skip to footer

How Import A Html Code To Jsf Page?

I'm trying to import this page to my JSF page which is gonna have database to get the data, to be more interactive. So I did this:

Solution 1:

This is not HTML code, this is JavaScript code. The JavaScript language has several operators which are special characters in XML (Facelets is a XHTML+XML based view technology), such as <, > and &. They needs to be escaped to &lt;, &gt; and &amp; to prevent the XML parser from literally interpreting them as XML.

for (var i = 0; i &lt; data.length; i++) {

An alternative is to wrap the entire JS code inside a <![CDATA[ ... ]]> block.

Better, however, is to put that JS code in its own JS file and include it by <h:outputScript>, e.g.

<h:outputScriptname="global.js" />

See also:


Unrelated to the concrete problem, you're not taking benefit of JSF resource management. I'd suggest to replace

<scripttype="text/javascript"src="../../resources/javascript/highchart/highcharts.js"></script><scripttype="text/javascript"src="../../resources/javascript/highchart/modules/exporting.js"></script>

by

<h:outputScriptname="javascript/highchart/highcharts.js" /><h:outputScriptname="javascript/highchart/modules/exporting.js" />

This way you don't need to fiddle with error prone URI-relative paths.


Update: as per comments, you'd like to pass some Java variables to the script file. You could do that by printing a Java object (which can be a javabean or a Map<String, Object>) as JSON object as if it is a global JS variable.

<h:outputScript>var data = ${bean.dataAsJson};</h:outputScript>

with e.g. (with help of Gson)

publicStringgetDataAsJson() {
    return gson.toJson(someBeanOrSomeMap);
}

(or just create the JSON object already during managed bean's (post)construction)

The script will be able to access it as data.someBeanPropertyOrMapKey.

Post a Comment for "How Import A Html Code To Jsf Page?"