tags:

views:

85

answers:

3

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).

+2  A: 

action 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>
pakore
Some say it's a bug, some say it's a feature of JSF 2 that you don't need faces-config anymore (at least for simple navigation cases), I think for not very complex applications it eases the burden of maintaining additional files - just like annotations for your managed beans... Just my 2c.
Gabor Kulcsar
Yep, you are right, I knew about it but I haven't used it yet. I use richfaces and to be able to use all the components, you need to disable the JSF2 VDL, that means that some things have to be done a-la-jsf1.2 yet.
pakore
Great, I'll add the navigation stuff in my faces-config, thanks!
Zenzen
+1  A: 

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?

Gabor Kulcsar
+1  A: 

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>
BalusC
Ah yes I know I should use the h:button, the thing is I'm using JSF2.0 and RichFaces 3.3.3 and with such combination from what I know I can't simply use h:button (and a few other new JSF2.0 tags).
Zenzen
You should not have tagged the question with `jsf-2.0`. I removed it. Still, it stands that it's a bad idea to use POST for page-to-page navigation. It's not SEO friendly (won't be indexed) and it's not user friendly (different behaviour when using browser back/forward buttons). Add `<redirect/>` to navigation case or use plain HTML.
BalusC