jsf

Jsf: passing data to another controller after post


In a legacy jsf application viewA (xhtnl page) uses controllerA, viewB uses controllerB. Both controllers are session scoped.

I have to add new functionality and a post on viewA should display viewB initialized with some data from controllerA. I tried with f:viewaction but it is executed only for get requests.

Maybe I could try with c:set or I could inject controllerA into controllerB but I'm searching for a cleaner solution.

Update

As stated above I unsuccessfully tried to pass data with f:viewaction but I forgot to mention that I intended to use Post redirect get pattern. Unfortunately in the post action I wrote something like viewB?faces_redirect=true. I didn't noticed that damn underscore was wrong


Solution

  • There are 2 problems:

    This causes that you cannot open and initialize the target page idempotently.

    If you cannot fix these 2 problems, then your best bet is really to simply inject one bean in another and trigger some initialization. You need to inject the bean of the target page (controllerB) into the bean where the action is invoked and then pre-initialize the bean of the target page in the action method, before navigating.

    public String submitAndNavigateToViewB() {
        controllerB.performInitializationFromControllerA(passingDataFromViewA);
        return "viewB";
    }
    

    But again, following the correct practices is better in long term. See the two links posted above.