1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1944414
  • 11
  • 1
  • 1
  • 4
  • what scope is your bean in? does your constructor get called every time? Maybe you van post some jsf xhtml source code? – roel Jan 03 '13 at 12:54

1 Answers1

1

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.

Community
  • 1
  • 1
Mohsen
  • 3,512
  • 3
  • 38
  • 66
  • Correct answer, but when I see sth like `fc.getExternalContext().getRequestParameterMap().get("printHello")` I'm thinking about abandoning the JSF – G. Demecki Jan 30 '14 at 13:11