user1944414
user1944414

Reputation: 11

GET request parameter not available by ExternalContext#getRequestParameterMap()

What I am trying to do is to be able to pass a parameter through an URL and execute certain actions if this parameter is being passed or not. I am using JSF + Richfaces.

For example if I try to access http:// localhost/myapp/home.jsf

public class My Bean {

private boolean printHello = false;

public MyBean(){
  FacesContext fc = FacesContext.getCurrentInstance();
  String printHello = fc.getExternalContext().getRequestParameterMap().get("printHello");
  if (printHello != null && printHello.equals("true")
    printHello = true;
}

public void myFunction() {
  if (printHello)
    System.out.println("test");
    //other stuff
    //ask for some user input
}

//When user validate his input, this function is called
public void myFunction2() {
  //some stuff
}

}

When I am asking for the user input in myFunction(), I also have a link on my page to start over the all process. If after clicking that link, and then manually change the url to http:// localhost/myapp/home.jsf?printHello=true

The bean won't be cleared and my printHello flag will still be set to false.

Also, when the following will be executed again:

FacesContext fc = FacesContext.getCurrentInstance();
String printHello = fc.getExternalContext().getRequestParameterMap().get("printHello");

printHello will be null, thats what I dont get. Maybe its due to the fact that not the all page is re-rendered?

Upvotes: 1

Views: 10792

Answers (1)

Mohsen
Mohsen

Reputation: 3552

JSF2 can handle GET parameters using <h:link> and <h:button> (check this page) If you need to handle GET parameters (bind them to your backing bean), here are two related questions:

  1. https://stackoverflow.com/a/7775203/141438
  2. https://stackoverflow.com/a/3355737/141438

Also in order to have fc.getExternalContext().getRequestParameterMap().get("printHello") work, move your code to a @PostConstruct annotated method instead of constructor and make sure your backing bean has a proper scope.

Upvotes: 1

Related Questions