Search code examples
javajsf-2primefacesprettyfaces

How to process prettyfaces 'query-param' in backing bean?


I have a list of users(dataTable with a link on userId that points to /user/view/{userId}). On clicking this userId link, the browser is redirected to the 'view' page as expected.

I have a page that accepts the url pattern http://localhost:8080/user/view/1 for first user and http://localhost:8080/user/view/2 for the second etc, but I don't know how to use the userId value once this page loads.

How can I achieve this with PrettyFaces URLRewriteFilter? How can I load data using the value of #{bean.userId} (1,2, etc) from the backing bean once PrettyFaces has injected it when the page is accessed. Can anybody explain?

<url-mapping id="view">
<pattern value="/user/view/#{bean.userId}/" />
<view-id value="/userview.jsf" />
</url-mapping>

I am using JSF2+Primefaces.3.0.M3+Prettyfaces-jsf2.3.3.2 with GAE.


Solution

  • You need a page-load action, specified by the <action> element in your URL-mapping configuration. First, you will need a method in your bean, like this:

    @Named("bean")
    @RequestScoped
    public class LoginBean {
    
    public String loadLoggedUser() {
        if ( userId != null ) {
            this.user = user.findById(userId);
            return null;
        }
        return "failure";
    }
    }
    

    Second, you will need to add the <action> to your URL-mapping:

    <url-mapping id="view">
         <pattern value="/user/view/#{bean.userId}/" />
         <view-id value="/userview.jsf" />
         <action>#{bean.loadLoggedUser}</action>
    </url-mapping>
    

    Here we have defined a page-action to be executed on a bean, #{bean.loadLoggedUser}, when a URL matching our pattern is requested. For example: /user/view/2.