3

I'm working with JSF 2.

For one of the pages, it needs to include a page along the lines of:

domain/subdomain/cms/[userspecified_code].html

I used the ui:include tag to get this file. It works fine for files that exists, but for files that doesn't exist, it throws in FileNotFoundException the renders the whole page as an error page.

Is there an alternate solution to the ui:include tag that skips/logs the file error, and just displays a empty section? Such that it minimises the disturbance to the user (the included file is only a small section of the page, if there's no matching file, I'll rather not show anything). One way I can think of is to have the section ajax loaded, so if there's an error, it'll be a javascript error rather than a sever error, but is there a more elegant/easier way of doing it?

Here the xhtml that I currently have:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:h="http://java.sun.com/jsf/html">

Lots of other html....

<ui:include src="domain/subdomain/cms/#{userspecified_code}.html"/>

Lots of other html....

</html>

Edit

Thanks for all the answers. I am looking for a solution that doesn't need me to add all the file checking logic in myself.

peterh
  • 11,875
  • 18
  • 85
  • 108
JackDev
  • 11,003
  • 12
  • 51
  • 68
  • Thanks for the answers so far, I'm looking for one where it's done automatically with out me having to write code logic to check myself. – JackDev Oct 22 '13 at 06:24

2 Answers2

6

The workaround you could use for this is checking if destination files exist at server side before rendering the page. Supposing you are using EL-2.2 which allows parameters for view methods, you could do something like that:

public boolean fileExists(String fileName)
    File file = new File(servletContext.getRealPath("domain/subdomain/cms/"+fileName+".html"));
    return file.exists();
}

And use jstl conditional tags to dynamically include the destination page:

<c:if test="#{bean.fileExists(userspecified_code)}">
    <ui:include src="domain/subdomain/cms/#{userspecified_code}.html" />
</c:if>

Also, in order to avoid code duplication, you might use fn:join tag to evaluate the path only once:

<c:if test="#{bean.fileExists(userspecified_code)}">
    <ui:include src="#{fn:join('domain/subdomain/cms/',fn:join(userspecified_code,'.html')}" />
</c:if>
Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
-2

You can write ui:include in a panel(primefaces).
And render(rendered is a attribute of panel) only if your fileName is not empty.

Adil Khan
  • 21
  • 5
  • Some thing like this: Lots of other html.... Lots of other html.... – Adil Khan Oct 14 '13 at 07:01
  • fileName is not empty doesn't guarantee that a file with that name exists – Vrushank Oct 14 '13 at 07:08
  • That will also fail if file doesn't exist. Due to the different cycles of JSF UIComponents and JSTL tags, `ui:include` [is always evaluated before](http://stackoverflow.com/a/14394963/1199132) the `render` attribute. `c:if` should be use for that instead. – Aritz Oct 14 '13 at 07:22