【问题标题】:JSF request scope with get parameter带有 get 参数的 JSF 请求范围
【发布时间】:2013-08-23 16:15:35
【问题描述】:

我知道有很多类似的主题,但不像我的:

我有一个 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);
    }
}

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

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

为什么会这样?

【问题讨论】:

    标签: jsf parameters parameter-passing managed-bean


    【解决方案1】:

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

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

    【讨论】:

    • 但是我的问题有什么解决方案吗?我怎样才能正确执行 doSomething() ?用户名和密码正确执行...只有 id 会出现问题(因为它没有文本字段或任何东西?
    • 选择更大的范围或pass the param to the bean
    猜你喜欢
    • 1970-01-01
    • 2011-11-11
    • 2011-08-15
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2015-02-27
    相关资源
    最近更新 更多