JSF 2.2 conditionally render HTML5 elements with jsf:rendered attribute - html

I d like to implement a new page in JSF 2.2 using HTML5 syntax.
My question: How can I achieve that a specific HTML element won't be rendered if jsf:rendered evaluates to false?
Sample:
<span class="msnumber" jsf:id="msnumber" jsf:rendered="#{!empty controller.msnumber}">[#{controller.msnumber}]</span>
Do i need to use a h:outputText for this purpose? I would appreciate it, if I could use the HTML elements directly.
Greets
Marc

There's nothing wrong in using HTML syntax into facelets directly. If you want to control it being rendered or not, your best is to use the <ui:fragment /> tag, which doesn't render anything by itself:
<ui:fragment rendered="#{!empty controller.msnumber}">
<span class="msnumber" id="msnumber">[#{controller.msnumber}]</span>
</ui:fragment>
Will render just
<span class="msnumber" id="msnumber">xx</span>
in case of #{!empty controller.msnumber} evaluating to true.
Regarding to the way you want to achieve it, I'm not sure about adding JSF component specific attributes to HTML tags directly (although being possible the other way). Honestly I've never seen that.

Your approach with the span as pass-through element should definitely work. I tried it in one of my JSF 2.2 projects and it works as intended with MyFaces 2.2.3 and Mojarra 2.2.6.
Do you use MyFaces or Mojarra? Which version? Try to use the most current one. HTML5 support is quite new and there are bugs in older versions of MyFaces and Mojarra.
Another possibility is, that there is something wrong with your EL expression #{!empty controller.msnumber}.

Related

Does Primefaces render invalid HTML?

To build a very simple web app for my company I'm evaluating some web frameworks, including PrimeFaces.
One strict requirement is the accessibility, and the fact that the HTML must be valid (checked against W3C Validator).
I've played a bit with the examples and I've noticed that the HTML rendered is not valid. The invalid block is the following:
<input name="javax.faces.ViewState" id="javax.faces.ViewState" value="2042368857675116551:8104174386673838460" autocomplete="off" type="hidden">
and the reason is:
line 74 column 159 - Errore: Attribute autocomplete not allowed on element input at this point.
So, can I perform some action on Primefaces in order to render valid HTML code? I didn't go deep into Primefaces, but I guess I have little control over how controls are rendered. Has anyone experience on this problem (validity of HTML rendered by PF) and would like to share it?
Thanks
The viewstate is not something that PrimeFaces adds to your rendered html but the jsf implementation. If you use mojarra there are some parameters that you can set to tune things (not tested this myself, just did some simple googling for you (hint, hint)).
See in How to let JSF render conform XHTML 1.0 strict?

How to set an empty attribute using Jade?

When using jade, AngularJs and angular-translate I prefer to use the translate directive as an empty attribute.
But for some reason, when using empty attributes in Jade, instead of getting something like <tag translate OR <tag translate='', I'm getting <tag translate='translate'
How can I add an attribute without a value?
The answer is to use the {doctype: "html"} when using the Pug (before was Jade) template engine.
This option has been documented recently, but it does not states what is it for.
Bear in mind that since I'm just processing all the jade files using gulp, I don't care that much about partials and such things...
Caveats: as stated by #lmacsen at github:
This fail if you need to use that code into a partial html file.
I've come up with this answer after reading several other github issues in the project page.
https://github.com/pugjs/pug/issues/201#issuecomment-1530205
https://github.com/pugjs/pug/issues/1180
I'm Using .foo(bar="") and it produces <div bar class="foo"></div>.
Using .foo(bar) you will get <div bar="bar" class="foo"></div>.
I'm Using most recent versions of Pug (formerly known as Jade) and I'm also using Partial jade files.

php echo equivalent in JSF

I would like to know if there is a php echo equivalent in JSF. I would like to publish html from my bean to my facelet.
This is because I want to add three different primefaces components each time a user clicks on one of the component that has been added. I could not achieve this with taglibs.
Thank you
I believe you got confused by builtin XSS prevention of JSF which caused your "plain vanilla" HTML to get escaped and thus displayed as-is instead of being interpreted as part of HTML source. Given that you're familiar with PHP, the explanation is that JSF has basically PHP's htmlspecialchars() by default turned on in all EL expressions printing output.
Just use <h:outputText> with escape attribute set to false to turn off implicit HTML escaping.
<h:outputText value="#{bean.html}" escape="false" />
Please make sure that you're fully aware of the potential XSS attack hole created here when it concerns user-controlled input. Your question at its own already indicates that you had no idea of it.
See also:
CSRF, XSS and SQL Injection attack prevention in JSF
Is it suggested to use h:outputText for everything?
As to the concrete functional requirement,
This is because I want to add three different primefaces components each time a user clicks on one of the component that has been added. I could not achieve this with taglibs.
Please note that JSF code is not HTML code. You should instead be writing those JSF components directly in the view and use the rendered attribute to render them conditionally.

Primefaces 3.0 RC <p:messages> HTML formatting is not working

I found out in primefaces.org that the version 3 Release Candidate of Primefaces is now ready for download, so I updated my website to use the new version. Unfortunately I the tag doesn't interpret the html tags that I put on the FacesMessage. Is there a way to enable it?
like for example i put something like this in my backing bean:
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, "","<b>Cannot load database driver.</b>"));
the bold HTML tag is being interpret as literals, it outputs the bold tag literally.
here a link to the primefaces forum about the same issue...
escape="false" in p:messages
not sure you gonna like the suggested ways to solve it there...

"title" attribute for <html:options> struts tag

In a regular select box you can show the full title on mouseover using "title" attribute in the "option" field.
In Apache Struts, you make the select box with the "html:options" tag. I don't think this tag supports the "title" attribute. Is there another way to show the full title of an option on mouseover for the "html:options" struts tag?
Thanks
Struts' tag for <select><option>.. is <s:select />, or <s:doubleselect /> for double lists. <s:doubleselect /> doesn't support title at all, you have to modify the Javascript it generates by hand (ddoubleselect.ftl).
For with the default theme, the file is found in struts2-core-2.1.6.jar under template/simple/.
hard code in html or jsp file is a bad code practice.
if you are writing code in struts 1.x try to store all messages in MessageResources_en.properties
try update your jsp with following (alternatively go for )
title="<bean:message key="your_tool_tip"/>
and modify MessageResources_en.properties
your_tool_tip=toolTIp is a cool idea
ps there are solutions available in jquery as well
I'll admit to not being familiar with Apache Struts. If nobody more familiar can find you a better solution then worst case you could do it as a javascript tooltip. It's more effort, but gives you more effective visual control over the presentation.