1

I have a simple JSF and managed bean and I need to make POST redirect when page render and some condition is true. JSF:

<h:panelGroup rendered="#{myBean.error=null}">
    <p>Some data</p>
</h:panelGroup>

In managed bean, the init() method, annotated as @PostConstruct and in this method I do

@PostConstruct
public void init() {
    if (someCondition) {
        FacesContext context = FacesContext.getCurrentInstance();
        String redirectUrl = "http://myurl.com";
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();

        try {
            ec.redirect(redirectUrl);
        } catch (IOException e) {
            e.printStackTrace();
            FacesMessage message = new FacesMessage(e.getMessage());
            context.addMessage(null, message);
        }
    }
}

but I need navigate user to redirectUrl with POST params and cannot find how to do it. With button it will be like this:

<form method='post' action="myUrl">
    <input type='hidden' name='param1' value='value1'/>
    <input type='hidden' name='param2' value='value2'/>
    <input name='button'  type='submit' value="Button">
</form>
Tiny
  • 27,221
  • 105
  • 339
  • 599
bearhunterUA
  • 259
  • 5
  • 15

1 Answers1

0

What you try to achieve is not possible that way.

A redirect will send a http 302 status code to the client with a location header including an url where to redirect to. Then the client will make a get request to that url and your post data will be lost.

There are alternatives to achieve this, here are some ideas.

  • You could forward the request using the ExternalContext.html#dispatch method, see this post for the differences. Note that this does not change the url in the browser's address bar.
  • You could store the post data in the user's session.
Community
  • 1
  • 1
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32