I want to put my JSF 2.0 xhtml files under WEB-INF\jsf. How do I access them then? I know that anything inside WEB-INF isn't exposed to the outside, so I need a controller to redirect me to the corresponding JSP, right? (This is also the model 2 pattern iirc).
Can I achieve this with a parameter inside web.xml/faces-config.xml? I think the FacesServlet is the controller of my webapp so it should serve this purpose?
And another question for understanding the Model 2 Pattern. Does every action have to go first to a servlet which then handles the next possible step? So a simple <a href="anotherPage.html" />
is forbidden in this pattern since it doesn't go to the controlling servlet?
You cannot. Files in
/WEB-INF
folder are not directly accessible.There are two options to workaround the problem of JSF source files being public accessible.
Map the
FacesServlet
on*.xhtml
instead of*.jsf
.Or, restrict direct access on
*.xhtml
by a<security-constraint>
inweb.xml
.See also:
The
FacesServlet
already does that. It's the controller. With JSF you already end up with a simple javabean as model and JSP/Facelets file as view. TheFacesServlet
as being the controller has already taken all the nasty work of request parameter gathering, validation, conversion, model updating and navigation from your hands.See also:
No, it's perfectly fine. The controller will kick in whenever needed. If the resource doesn't need a controller (i.e. static resource), then you also don't need to let it pass through some controller.
In the future, please ask multiple questions in separate Stack Overflow questions.
To access
xhtml
pages insideWEB-INF/jsf
folder you may do next:xhtml
pages folder fromwebapp root
toWEB-INF
url
based to pages from application.xhtml
"WEB-INF/jsf/<name>.xhtml
"jsf ViewHandler getActionUrl
to exclude "WEB-INF
" from generatedaction
url
(ofform, link, button
)For example,
xhtml
pages are in webapp root folder "jsf
". Allurl
between pages are likejsf/<pageName>.xhtml
. So we do next:move
<webapp root>/jsf
to<webapp root>/WEB-INF/jsf
create FrontController servlet:
``
web.xml
tourl
based for pages:web.xml
to.xhtml
request
to correctxhtml
page:``
So if url for page was
/myapp/jsf/home.xhtml
then Dispatcher will forward it tomyapp/WEB-INF/jsf/home.xhtml
. And Faces Servlet will handle ".xhtml
" request. But if on a page are usedjsf
components likeh:form, h:link, h:button
etc which generateaction
orurl
then theurl
will be really including "/WEB-INF
". So to exclude it we need next step.Exclude "
/WEB-INF
" fromjsf
generatedurl
(for jsf form, link, button). For that:6.1 create subclass of
jsf ViewHandler
and overridegetActionUrl
:``
6.2 configure
jsf
to use the specifiedViewHandler
. Infaces-config.xml
add next: