Does JSF 2.0 strip out HTML attributes when rendering? - jsf-2

I have to work with existing HTML and CSS and convert it to JSF app. So there are pure <li> elements (no JSF tags) with class="" attributes. When rendered with JSF 2.0 under Glassfish 3.1.1 the class="" attribute is removed from the <li> elements and the CSS breaks, i.e. the site breaks.
Probably the setting class="" is not the best to do but why does JSF strip it off when it affects the display of the page?

Facelets does only do that for attributes with empty values. It does not do that for attributes with a value like class="some". So nothing would break at all (expect of some hypothetically poor JS which rely on the presence of the attribute instead of the presence of an attribute value).
Note that GF 3.1.1 ships with JSF 2.1, not JSF 2.0 (to be precise, Mojarra 2.1.3).

use <f:verbatim> tag
<f:verbatim>
<div class="" custom-attribute="x"></div>
</f:verbatim>
this would print content without filtering.

Related

JSF 2.0 h:datatable not using supplied id attribute

I'm observing the following behavior within a JSF 2 page (Mojarra 2.1.18 / RedHat EAP 6.1, if that's useful). I've got a form wrapping a data table, and I'm supplying an ID attribute value for both the form and the table. When I view the resulting html source file, I see that the table ID is set to an auto-generated value and the form ID attribute is not prepended to the auto-generated table ID. That is:
This set of tags:
<h:form id="form4">
<h:datatable id="notices" ...>
...
</h:datatable>
</h:form>
Yields the following html:
<form id="form4" ...>
<table id="j_itd68"> //i.e. id != "notices"
...
</table>
</form>
There are more elements in the JSF xhtml file preceding the h:form/h:datatable, but I've intentionally excluded them here (hoping that someone might recognize this symptom without additional clutter). Things go wrong for me when I attempt to include some PrimeFaces p:commandbutton instances within the table. They don't get named properly (i.e. they don't include the enclosing form ID in the generated ID), and this causes a "component not found for ID" servlet error. The same improper naming occurs if I replace the p:commandbutton instances with h:commandbutton instances (so I don't believe this is a PrimeFaces issue). This behavior seems like the result of a malformed JSF page, but I haven't found anything yet (a NetBeans XML check on the JSF xhtml file returns a successful result). Any help is appreciated.
Best regards,
-Andy

EL is not working in a jsp page

I have a JSP page,In that page,I am trying to use the page scope attributes using jstl and struts2 tags.
The following piece of code is,
<%# taglib uri="/struts-tags" prefix="s" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="test" value="ramesh"/>
<c:set var="test1" value="${test}"/>
<s:set var="test2" value="${test}" />
the <s:set> tag yields the following exception " According to TLD or attribute directive in tag file, attribute value does not accept any expressions".
I have two questions.
1)${test} works when it is used in <c:set> tag.
2)${test} does not works when it is used in <s:set> tag. Why?
EL and JSTL are now in Java EE standards, so they can cooperate well, I think.
However, when in struts 2 tags, because struts 2 have their expression language - OGNL,
so I think they prefer to use OGNL to EL in their tags, and that is the reason why they do not support EL. These are my guess.
From Apache link
From Apache FAQ link
As of Struts version 2.0.9 the JSTL/JSP expression language (EL) has been disabled for Struts tag attributes which evaluate OGNL. This is a precaution against security vulnerabilities that can result from the double-evaluation that occurs when an attribute is first processed as a JSTL/JSP EL expression and then the result is processed as an OGNL expression. The solution is to express all dynamic attribute values in Struts tags using OGNL expressions directly.

Thymleaf: Adding attribute in div tag

I need to add an attribute in with in div tag in thymleaf as follows
<div class="mp_snippet_address" itemscope itemtype="http://schema.org/LocalBusiness">
the problem is i could not define the itemscope in div tag
The standard HTML parser in Thymeleaf is a very strict XML parser. You can switch to the less-strict LEGACYHTML5 template mode (then include the nekoHTML 1.9.15+ as a dependency), which allows for 'boolean attributes', but the output may be slightly different than the input templates as I think it performs some kind of tag balancing to massage HTML5 into being well-formed XML.
Support for HTML as per the HTML spec is on the cards for Thymeleaf 3.0, but that version is still a very very very long way away.

Refer to JSF dynamically generated ids based on iteration index

In JSF, <ui:repeat/> and similar components such as PrimeFaces <p:dataTable/> generate dynamic ids for sub-components based on the iteration index, i.e.:
<p:dataTable id="table" var="item" value="#{itemList}">
<h:outputText id="name" value="#{item.name}"/>
</p:dataTable>
will generate something like this:
<table id="table">
<span id="table:0:name">name0</span>
<span id="table:1:name">name1</span>
<span id="table:2:name">name2</span>
...
<span id="table:n:name">nameN</span>
</table>
so all the elements clearly have a distinct client id. I intentionally skipped the <tr/>, <td/>, etc.
So, <p:ajax ... update=":table:name"/> refers to all the names in the table and it works fine, <p:ajax ... update=":table:#{someDesiredIndex}:name"/> fails with a message similar to SEVERE: javax.faces.FacesException: Cannot find component with identifier ":table:0:name" in view. event though I can confirm that the component exists in the markup. Is it not possible to do this?
I'm running on GlassFish 3.1.2 and Mojarra 2.1.6 in case it is relevant.
It does indeed not exist in the JSF component tree as traversable by UIViewRoot#findComponent(). It exists only in the generated HTML output. There's only one <h:outputText id="name"> in the JSF component tree, not multiple as you seemed to expect. It's just been reused multiple times when producing the HTML output. At best, you can get the physical component by table:name, but this does in turn not exist in the HTML DOM tree, so the document.getElementById() would fail on that during performing the ajax update.
In order to achieve the concrete functional requirement anyway, you basically need to have a physical existing component representing the row in the JSF component tree. You can create them in a loop if you use a view build time tag, such as JSTL <c:forEach>, instead of a view render time tag.
<table id="table">
<c:forEach items="#{itemList}" var="item" varStatus="loop">
<tr><td><h:outputText id="table_#{loop.index}_name" value="#{item.name}" /></td></tr>
</c:forEach>
</table>
This will create physically multiple components in the JSF component tree and this get rendered as:
<table id="table">
<span id="table_0_name">name0</span>
<span id="table_1_name">name1</span>
<span id="table_2_name">name2</span>
...
<span id="table_n_name">nameN</span>
</table>
And you can reference them via e.g. update=":table_#{someDesiredIndex}_name".
See also:
How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
JSTL in JSF2 Facelets... makes sense?
Update: since Mojarra 2.2.5, the <f:ajax> doesn't validate the client ID anymore and the renderer is capable of walking through iterating components in order to find the right iteration round to render. So referencing the iteration index in <f:ajax> this way should just work fine. It only doesn't work yet in current MyFaces 2.2.7 / PrimeFaces 5.1 versions, but it's expected that they will catch up it in a future version.

f:verbatim tag stops working when inside a dataTable

I posted this to the PrimeFaces user forum but I think they are too busy to look into it, so I thought I would try here.
I have server-side string that has markup in it, so when I want it rendered I do this:
<p:panel>
<f:verbatim>
#{daBean.markedUpString}
</f:verbatim>
</p:panel>
This works fine, but not if the same tag is used inside a p:dataTable -- either with or without the p:panel enclosure. What gets rendered is a div class="ui-dt-c" element with nothing in it. To test, if I take out the f:verbatim tag the marked-up text gets escaped and rendered.
I don't know if this should be considered a bug or not, but does anyone know of a work-around for this? This is with PrimeFaces 3.0.M3.
The <f:verbatim> tag is intented to hold plain text/HTML, not JSF components nor EL expressions. The tag is a leftover from JSF 1.0/1.1 ages when it was not possible to inline plain text/HTML between JSF components. The tag is deprecated in JSF2. You do not need it anymore.
Your concrete functional requirement is thus displaying some HTML string from a managed bean unescaped. For that you should use <h:outputText> with escape="false".
<h:outputText value="#{daBean.markedUpString}" escape="false" />
See also:
Getters inside f:verbatim called before form submission
JSF/Facelets: why is it not a good idea to mix JSF/Facelets with HTML tags?
What are the main disadvantages of Java Server Faces 2.0?

Resources