2013-08-23 93 views
1

我知道有很多类似的线程,但没有像我这样的:用GET参数JSF请求范围

我有一个requestscope豆:

@ManagedBean 
@RequestScoped 
public class Bean implements Serializable{ 
    private String username = ""; //managed by textbox 
    private String password = ""; //managed by textbox 

    private String id ="-"; 

    //Load the Parameter as usual: 
    @PostConstruct 
    public void fetchParams(){ 
     System.out.println("FETCH PARAMS"); 
     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     String id = facesContext.getExternalContext().getRequestParameterMap().get("id"); 
     if(id == null || id.length() == 0) return; 
     setId(id); 
    } 

    // getters & setters 

    public void doSomething(){ //executed when clicked on the sumbit-button on the jsf-site 
     StaticFunctions.doSomething(this); 
    } 
} 

代码并执行以下操作: 它检索的get参数“id”并将其保存到String id中(由string.out ....确认)。

但是,当方法doSomething()被执行,并且先前存储的“id”被读取并且返回“ - ”(没有任何事情发生)。

这是为什么呢?

回答

2

您的ManagedBean为@RequestScoped,并在请求结束时被销毁。当doSomething()被执行时,用户提交表单并开始一个新的请求。 因此,您应该在控制台中看到“FETCH PARAMS”两次,因为已创建两个Bean,但对于第二个请求idnull

你可以找到关于四个JSF-范围here的详细解释。

+0

但有没有解决我的问题?我如何正确执行doSomething()?用户名和密码是否正确执行...只有id会造成问题(因为它没有文本框或任何内容?) – Niko

+0

选择更广泛的范围或[将param传递给bean](http://www.mkyong.com/JSF2/4-方式到传递参数从 - JSF的页面到支持bean) – lefloh