5

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

Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • `param.target` seems to hold the `-en` value , which means its not empty and that's why you are refered to `-en.html` page... seems that there an issue with the value that being hold by `param.target` – Daniel May 30 '12 at 07:46
  • @Daniel: oops, sorry, the `-en` should not be there. I missed that when I was simplifying my code for posting here. – Mr.J4mes May 30 '12 at 07:47

2 Answers2

4

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

<ui:include src="#{empty param.target? 'About' : param.target}.html" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 2
    You can't use `+` to concatenate strings in EL. I fixed the syntax. – BalusC May 30 '12 at 14:00
  • @BalusC , say why does my proposed solutio does not work in this scenario ? cause it always works just fine for me... – Daniel May 30 '12 at 14:07
  • 2
    @Daniel: EL in `src` of `` is evaluated during view build time, not during view render time. It works only if the `src` points a valid path. – BalusC May 30 '12 at 14:08
3

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

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Even though NetBeans suggested that `ui:include` has `rendered` attribute, it seems that you may be right. However, I still run into the same exception using your solution. – Mr.J4mes May 30 '12 at 08:34
  • try to output the param.target in your page... what does it show you? `` do you see anything between prefix and suffix ? – Daniel May 30 '12 at 08:39
  • I did try to replace `` with `` in your above code. It printed out the correct message when `param.target` is null and not null. I think Joop Eggen is right. The `src` attribute was evaluated in both cases. – Mr.J4mes May 30 '12 at 08:46
  • I always use the wrapping with `h:panelGroup` and it always worked here... I wonder if – Daniel May 30 '12 at 08:55
  • As I told you, I tried to put `` inside `` and it worked perfectly. The problem only occurs if I put `` inside the ``. :) – Mr.J4mes May 30 '12 at 10:37