【发布时间】:2013-01-03 04:44:33
【问题描述】:
我想要做的是能够通过 URL 传递参数,并在是否传递此参数时执行某些操作。我正在使用 JSF + Richfaces。
例如,如果我尝试访问 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
}
}
当我在 myFunction() 中要求用户输入时,我的页面上还有一个链接可以重新开始整个过程。如果点击那个链接后,再手动将url改成http://localhost/myapp/home.jsf?printHello=true
bean 不会被清除,我的 printHello 标志仍将设置为 false。
还有,什么时候会再次执行:
FacesContext fc = FacesContext.getCurrentInstance();
String printHello = fc.getExternalContext().getRequestParameterMap().get("printHello");
printHello 将为空,这就是我不明白的。可能是因为没有重新渲染所有页面?
【问题讨论】:
-
你的 bean 在什么范围内?你的构造函数每次都会被调用吗?也许您可以发布一些 jsf xhtml 源代码?
标签: jsf http-request-parameters