tags:

views:

145

answers:

2

I have some JSF xhtml pages like below.

ROOT
|
|--index.xhtml
|
|--register.xhtml
|
|--<templates> (Directory)
|   |
|   |--userForm.xhtml
|   |
|   |--banner.xhtml

both pages are using the templates from templates directory. My index file is getting the templates well and works fine. I have a link from index.xhtml file to register.xhtml. My register.xhtml is displayed like an xml file. The reason is ttemplate is not applied to that.

But if I call the register.xhtml like (faces/register.xhtml) then it displays correctly.

What would be the reason? is there any workaround to solve it?

+2  A: 

The URL of the link has to match the url-pattern of the FacesServlet as definied in web.xml to get JSF to run. If it is for example *.jsf, then the link should point to register.jsf and not register.xhtml. Alternatively, if you're using a JSF link component like <h:link>, then you can also use the filename alone as outcome and JSF will worry about generating the right URL.

<h:link outcome="register">

That said, you'd probably like to prevent endusers from directly accessing XHTML files by URL, in that case you can add a security-constraint on the url-pattern of *.xhtml with an empty auth-constraint in web.xml which prevents that:

<security-constraint>
    <display-name>Restrict direct access to XHTML files</display-name>
    <web-resource-collection>
        <web-resource-name>XHTML files</web-resource-name>
        <url-pattern>*.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint />
</security-constraint> 
BalusC
+1 for Security Constraint info.
gurupriyan.e
@guru: It's very useful, yes :) It's unfortunate that JSF doesn't support having templates in `/WEB-INF`. It's technically not impossible.
BalusC
THanks BalusC. I will try this out and back to you. Keep in touch.
Muneer
Hi dear,Now I added my link using <h:link /> tag. The links work fine. Still I have one question. The link is automatically go into /faces/register.xhtml . Is there any way that I can hide the /faces/ directory and link should displayed /register.xhtml without the "faces" directory???
Muneer
Ah, you're using prefix mapping `/faces/*`, no you can't hide it. Use extension mapping like `*.jsf`, `*.faces` or even `*.customextension`.
BalusC
How to do that please?
Muneer
Replace `/faces/*` by `*.jsf` as `url-pattern` of `FacesServlet`.
BalusC
A: 

As BalusC said, you might have configured your web.xml as *.faces but when the link is clicked in index.xhtml , the actual href does not have faces/ before it.

gurupriyan.e