Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0: Empty operator doesn't work with param

Tags:

java

el

jsf-2

param

In the file aPage.xhtml, I have the following lines:

<ui:include rendered="#{not empty param.target}" src="#{param.target}.html" />
<ui:include rendered="#{empty param.target}" src="About.html" />

With the above lines, I expected that when I go to http://localhost:8080/beta/aPage.xhtml, the page About.html would be included since the param.target is null. However, GlassFish threw me the following exception:

java.io.FileNotFoundException: http://localhost:8080/beta/.html

Somehow, param.target was not considered to be null.

Besides, I did try to use == and != operators as following:

<ui:include rendered="#{param.target != null}" src="#{param.target}.html" />
<ui:include rendered="#{param.target == null}" src="About.html" />

Interestingly, this time, on the console of GlassFish, I didn't see any exception thrown. However, on the browser, an error page still appears with the exception java.io.FileNotFoundException.

I'd be very grateful if you could tell me why this happened and what I should do to avoid it.

UPDATE:

Thanks to the hint from Joop Eggen, I finally solved the problem with the following lines:

<ui:param name="noTarget"  value="About.html" />
<ui:param name="hasTarget" value="#{param.target}.html" />
<ui:include src="#{empty param.target? noTarget : hasTarget}" />

Best regards

like image 691
Mr.J4mes Avatar asked May 30 '12 07:05

Mr.J4mes


2 Answers

The src is evaluated in both cases, maybe with a file existence test? Do

<ui:include src="#{empty param.target? 'About' : param.target}.html" />
like image 126
Joop Eggen Avatar answered Oct 21 '22 10:10

Joop Eggen


ui:include got no rendered attribute... wrap it with <h:panelGroup

like this

<h:panelGroup rendered="#{not empty param.target}">
   <ui:include  src="#{param.target}.html" />
</h:panelGroup>
<h:panelGroup rendered="#{empty param.target}" >
    <ui:include src="About.html" />
</h:panelGroup>

Edit

Unfortunately this will work only when EL in src of points a valid path ,

cause

EL in src of <ui:include> is evaluated during view build time, not during view render time

like image 24
Daniel Avatar answered Oct 21 '22 11:10

Daniel