Navigation in JavaServer Faces, redirecting with correct parameters
I have a page: http://mypage.com/items.jsf.
This page takes the following for granted:
- ID is set by GET or ID is set by POST.
Now, I can manually call items.jsf?ID=10
But what I really want to do is have a Button which calls a function that returns the navigation URL.
public String test()
{
return "10";
}
Now having the following code in the JSF file.
<h:form>
<h:commandButton action="#{itemsBean.test}" value="Redirect me" />
</h:form>
What I want to happen is that when I press "Redirect me", I want a Navigation-Rule to know that I want to go to: items.jsf?ID=10
Is this possible? 开发者_JAVA技巧It feels like it's a trivial problem really.
With only the core JSF framework and the Facelets library, you may find this difficult. The JSF 1.2 library does not include support for adding request parameters to the redirect URL.
A few solutions spring to mind:
- Just redirect as outlined here.
- Add your own custom NavigationHandler and/or ViewHandler code. Some 3rd party frameworks already cater for this type of thing. For example, Seam adds a pages.xml that allows parameters in navigation rules.
- Upgrade to JSF2. It adds support for handling parameters during navigation (and standardises Facelets as part of the core API, among other things - spec here). It's at release candidate 2, which may or may not suit.
If you are stuck with JSF1.2, you can also use the <h:outputLink> and nest <f:param name="" value=""> tags within it. The outputLink will include the parameters as query-params: ?name=value
If you need page-actions, navigation from methods, or jsut want to let an extension manage this for you, you can use PrettyFaces – SEO, Dynamic Parameters, Bookmarks, and Navigation for JSF / JSF2
It includes much simplified JSF navigation and linking.
精彩评论