Server action and redirection

Splash Forums PrettyFaces Users Server action and redirection

This topic contains 7 replies, has 3 voices, and was last updated by  Christian Kaltepoth 10 years, 4 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
  • #18030



    I want to redirect a user to a target page, after some server action on the current page :

    I have a form witch collect information on a “New project” then, I have a commandButton (or everything else witch can call server action method) witch call my BackingBean method “createNewProjectInDataBase”. You can easily guess what the method do…

    My problem is to redirect the user after the insertion in my database to create some “project steps”.

    In my app architecture, I want this type of URL “/admin/project/#{projectid}/step/create

    How after the database insertion, i can redirect the user to the right URL containing the #{projectid} of the project I just created ?

    I tried a lot of things I found in the doc with no success :(

    Someone can help me ?

    Thanks by advance

    PS : Sorry for my poor english. Hope I don’t make to much mistakes

    Configuration :

    Tomcat 6.0.33

    JSF Moraja implementation

    PrettyFaces 3.3.0

    Primefaces 3.0.M3


    I am assuming that you are using JSF2, and that you are not using value-injection in your URL-mappings.

    <url-mapping id="viewProject">
    <pattern value="/admin/project/#{projectid}/view" />
    <view-id value="/admin/project/view.xhtml" />

    PrettyFaces needs two things in order to be able to build your URL:

    1. It needs to know which url-mapping to send to

    2. It needs to know what the value of #{projectId} should be

    You can do this by returning the path to the Facelets view file, and adding a query-parameter with the value of projectId, like so:

    return "/admin/project/steps/create.xhtml?projectId="+project.getId()+"&faces-redirect=true";

    PrettyFaces will intercept that redirect and send to the proper page, as long as the view matches the view that was configured in your prettyfaces <view-id>.

    Otherwise, if you are using parameter injection like below, you can simple set the value of projectId in your backing bean, and then return the ‘pretty:mappingId’

    <url-mapping id="viewProject">
    <pattern value="/admin/project/#{projectBean.projectId : projectid}/view" />
    <view-id value="/admin/project/view.xhtml" />

    public String createNewProjectInDatabase()
    // create project
    projectId = project.getId();
    return "pretty:viewProject";

    I hope this helps.



    Wow !

    That’s a fast response ! Thanks for that !

    Yes, I use 2.0 implementation

    For your first solution, I’ve followed the doc and i tried :

    String rewrittenURL = FacesContext.getCurrentInstance().getExternalContext()

    Unfortunately, it’s doesn’t works.

    Your solution(return “/admin/project/steps/create.xhtml?projectId=…”;) is working, i’m redirect to the correct page.

    Unfortunately my browser address bar indicate “…./jsp/admin/etapeprojet.xhtml?codeprojet=1”.

    That’s not pretty :(

    For your second solution, I don’t think I really understand injection management in this case…

    I have a controller bean by jsf file : NewProjectController/newProject.xhtml and NewStepController/newStep.xhtml

    I don’t understand how to inject the projectId newly created in NewProjectController in NewStepController and simply return “pretty:createNewStep”.

    I want to use standalone view/controller and don’t create dependency between controller.

    I think in your case, you create a dependency beetween the “NewProjectController” and your “ViewController”.

    I’m in the wrong way ?


    Just a small note on the second approach:

    Yes, you are understanding it correctly. The typical way of doing navigation with this approach is to get a reference to the bean “responsible” for the second page and setting the corresponding properties. If you then return pretty:someId from the action method, PrettyFaces will know which mapping you want to redirect to. PrettyFaces will then read the path parameter values from the second bean and build the correct URL.

    You are 100% correct if you say that this creates a dependency between the two page beans. But I for myself prefer this way over the first one because it is more type-safe! And I don’t have to build URL using string concatenation. But that’s just my personal opinion on this! :)



    Thanks for your answer !

    I understand your opinion about type-safe but there still a problem.

    A REST application is normally totally stateless. In your case, the page “referenced” is only accessible via the “responsible” page.

    What append if you call directly the “referenced” page in your browser ?

    Values from the “responsible” page’s controller are not initialized, so you’ll get a NullPointerException ?

    I don’t have my project on this computer, i’ll try tomorrow.


    No, the pages are independent. You are just setting the properties in the foreign bean to allow PrettyFaces to generate the correct URL to redirect the user to. Let me show this in an example.

    Imagine a form some user submits and you want to take the user to a product details page after that.

    Request 1 (POST-Request: The user submits the form)

    – Your action method gets invoked

    – You do whatever you want to do to process the postback

    – Set the productId property on the ProductDetailPageBean

    – Return pretty:productDetails

    – PrettyFaces reads the productId from the bean and generates the target URL.

    – PrettyFaces redirects the user to: /product/123456

    Request 2 (GET-Request: The browser requests /product/123456)

    – PrettyFaces injects 123456 back into the ProductDetailPageBean

    – Your action method of ProductDetailPageBean gets invoked

    – More…

    You see how it works. It’s just an implementation of the Post-Redirect-Get Pattern [1].





    Thanks Lincoln and Christian. It works perfectly !

    I tried using parameter injection and it works like a charmed !

    PrettyFaces is definitely like Facelets, it may be directly include in JSF !

    Thanks for your work !


    You are welcome! :)

    Feel free to post on the forums again if you have any further questions!


Viewing 8 posts - 1 through 8 (of 8 total)

The forum ‘PrettyFaces Users’ is closed to new topics and replies.

Comments are closed.