jsfprettyfaces

jsf: get id from rest url or rewrite url


Currently I rewrite url as below

.addRule(Join.path("/users").to("/views/user/index.jsf"))

But now I need to rewrite url which includes numbers(user id). For example, if I have url as /users/1 or /users/200, etc. I need to match them to view /views/user/profile.jsf

Similarly, I also need to rewrite urls /users/1/edit or /users/200/edit to /views/user/edit.jsf

How can I achieve this?


Solution

  • This is what is working for me now but not sure if this is the correct way of doing things.

    First I rewrite the url as .addRule(Join.path("/users/{id}").to("/views/user/show.jsf"))

    and I make the url as

    <h:column>
                    <f:facet name="header">Show</f:facet>
                    <h:outputLink value="/users/#{user.id}">Show</h:outputLink>
                </h:column>
    

    This generate the url as /users/1 or /users/200 etc.

    Now I can access the user id parameter from jsf page show.xhtml as <h:outputText value="#{param['id']}" />

    I can also access the parameter from bean as

    public void show(){
        Map<String, String> params = FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
        String user_id = params.get("id");
    }
    

    Or I can also do something like this

    in show.xhtml as

    <ui:param name="user" value="#{userController.show(param['id'])}" />
    
    Name: #{user.firstName}
    

    and in userController as

    public User show(Integer id){
        user = userService.findById(id);
        return user;
    
    }