I'm having a little bit of a problem here, whenever I use an action attribute (i.e. <h:commandButton action="/test/test2.whatever" value="Test"/>
) jsf changes the extension to .jsf and then redirects me there. So if I press the aforementioned button it would redirect me to /test/test2.jsf. Is this the default behavour and can I change it so only for example files with .xhtml would be changed to .jsf (to be honest this is my first JSF project and the configuration was done by my teammate who's on vacation).
views:
85answers:
3action
should return a string that will be used by the NavigationHandler
to decide the next page to render. These navigation rules are defined in the faces-config.xml
file, located normally under /WEB-INF
.
Besides, there is a context-param
in the file web.xml
defining what is the default extension of JSF files. I.e.
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
So probably yours is defined to be .jsf
and that's why JSF is changing your .whatever
to .jsf
My recomendation is that you shouldn't decide navigation rules inside the action
parameter. Instead you should define them through the faces-config.xml
file. I.e:
<navigation-rule>
<display-name>login</display-name>
<from-view-id>/pages/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>userLoaded</from-outcome>
<to-view-id>/pages/user.xhtml</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-outcome>userLoadedFail</from-outcome>
<to-view-id>/pages/login.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
Another thing (other than the default suffix in Pakore's answer) to consider would be the servlet mapping in web.xml:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
So be careful, if the JSF servlet is mapped by the .jsf extension you wouldn't want to change that... otherwise how would it know that the page is a JSF page?
The h:commandButton
renders a POST button. It is really not recommended to use POST for page-to-page navigation. Rather use the h:button
, it renders a GET button. You can just specify the "view ID" in the outcome
(basically the filename part without extension).
<h:button outcome="test2" />
It will implicitly go to test2.xhtml
. No need for a navigation case.
However, if the target page is NOT a JSF page, then you don't need a JSF button here. Just plain vanilla HTML suffices.
<form action="page.html"><input type="submit" /></form>