0

I have a commandButton that I will use to navigate and send a parameter to another page (the id of the selected object so I will be able to load the entire object in the other page after retrieving it from the database). I know that commandButtons use POST as request method and that it isn't a good practice for navigation, but in my tests I was close to the expected results using it instead of any other methods.

The code of my commandButton is something like this:

<p:commandButton value="Start" action="#{startBean.goToNextPage()}">
    <f:param name="objectId" value="#{startBean.object.Id}"/>
</p:commandButton>

The problem happens on page load (specifically when the commandButton is rendered), because executes before the object be set, when the navigation occurs the parameter is sent to the other with 'null' value.

So, I'd like to know how I can set the 'value' only when click on the commandButton, or other ways to navigate to the other page sending parameters, in this specific case it might be using GET or POST.

Thanks in advance!

UPDATE

I'll detail what I'm really trying to do. I'm trying to send a simple parameter (an object id) as POST or GET while navigating to another page. I tried first with <p:commandButton /> for stylish reasons. The action attribute of the commandButton calls a method from managedBean that returns the url which it should navigate to. The goToNextPage() looks like:

public String goToNextPage(){
    String url = "nextPage.xhtml?" + "faces-redirect=true&objectId=" + this.object.getId();
    return url;
}

After the gotToNextPage() method the @PostConstruct of the nextPageBean runs and I'm able to read the parameter sent using FacesContext#getRequestParameterMap(), the navigation doesn't complete, and the page just refresh.

So I'd like to know how I should send parameters to another page and navigating to it using GET or POST.

  • Since the object is already present in your bean, why can't you simply access it in `goToNextPage` method? – Jasper de Vries Feb 11 '18 at 17:42
  • Why not? What does your `goToNextPage` method look like? Please update your question. – Jasper de Vries Feb 15 '18 at 11:25
  • @JasperdeVries I'm able to acces in the goToNextPage() method, the issue is that I'm not able to send the object id to the next page using an URL, for example: `"nextPage.xhtml" + this.obj.getId();` probably because it's a commandButton (that uses POST not GET request). – Eduardo Dorneles Feb 15 '18 at 11:33
  • Possible duplicate of [How do you use JSF implicit redirection with POST parameters](https://stackoverflow.com/questions/14053904/how-do-you-use-jsf-implicit-redirection-with-post-parameters) – Jasper de Vries Feb 15 '18 at 11:39
  • I've tried to do what you suggested in this answer, but it didnt work, nothing happens after the ExternalContext.redirect() method. I also tried using the `?faces-redirect=true`, while debugging, the execution goes to the next ManagedBean (the bean of the next page), but "navigation" doesn't occur, actually only some kind of refresh happens and it stays on the same page. – Eduardo Dorneles Feb 15 '18 at 12:16
  • 1
    Again, what does your `goToNextPage` method look like? Please provide a [mcve]. – Jasper de Vries Feb 15 '18 at 12:31
  • @JasperdeVries I have updated the post. Thanks. – Eduardo Dorneles Feb 19 '18 at 16:54
  • Try without `.xhtml`. See https://stackoverflow.com/questions/9463295/jsf-redirect-to-other-page – Jasper de Vries Feb 20 '18 at 16:04

0 Answers0