1

我知道有很多类似的线程,但不像我的:

我有一个 requestscope bean:

@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);
    }
}

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

但是当执行 doSomething() 方法并读取先前存储的“id”并返回“-”时(就像什么都没发生一样)。

为什么会这样?

4

1 回答 1

2

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

您可以在此处找到有关四个 JSF-Scopes 的详细说明。

于 2013-08-24T07:28:28.720 回答