Search code examples
jsfnavigationhttp-request-parameters

Implicit Navigation - GET parameter not set


I have a method in one of my ManagedBeans which will redirect to another page, it is also supposed to append an id to the URL.

Eg.

public String editForm(String formId) {
    return "designer?id=" + formId;
}

I call this from my main page like so

<p:menuitem value="View/Edit" icon="ui-icon-search"
    action="#{formsView.editForm(formsView.selectedForm.id)}" />

Then I have a @ViewScoped bean that is used in the designer page, and in it's @PostConstruct I have something like this

@PostConstruct
public void init() {
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    String formId = params.get("id");
    ...
}

However the id key doesn't seem to appear in the params Map, what am I doing wrong?


Solution

  • ... which will redirect to another page ...

    You're actually not performing a redirect. You're performing a forward. You're actually not creating a new HTTP request with the parameter therein. You're displaying the target page in the HTTP response of the very same HTTP request you're currently sitting in. To learn the difference, head over to What is the difference between redirect and navigation/forward and when to use what?

    You need to perform a real redirect. You need to create a brand new HTTP request. You can do that by appending the predefined faces-redirect=true parameter to the query string.

    public String editForm(String formId) {
        return "designer?faces-redirect=true&id=" + formId;
    }
    

    You can confirm its working by looking at the browser's address bar. The id parameter must appear over there in order to get it to end up in the request parameter map.

    If you however intend to hide away it from the URL, and thus you actually didn't want to perform a redirect at all, but a true forward, then you should be looking for a different approach to pass data along: Pass an object between @ViewScoped beans without using GET params.