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

1.9k views Asked by At

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.

2

There are 2 answers

0
Kushan On BEST ANSWER

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.

2
Hemant Metalia On
<url-mapping id="login">
        <pattern> /user/view/1 </pattern>
        <view-id> /legacy/user/login.jsp </view-id> <!-- Non JSF View Id -->
    </url-mapping>
    <url-mapping id="register">
        <pattern>/user/view/1 </pattern>
        <view-id>/faces/user/register.jsf</view-id> <!-- JSF View Id -->
    </url-mapping>
<url-mapping id="login1">
        <pattern> /user/view/2 </pattern>
        <view-id> /legacy/user/login2.jsp </view-id> <!-- Non JSF View Id -->
    </url-mapping>
    <url-mapping id="register2">
        <pattern>/user/view/2 </pattern>
        <view-id>/faces/user/register2.jsf</view-id> <!-- JSF View Id -->
    </url-mapping>

refer http://ocpsoft.com/prettyfaces/