2

所以我有这个非常小的 JSF 示例:

<!DOCTYPE html>
<html xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Index</title>
</h:head>
<h:body>
    <h:form>
        <h:commandButton action="#{reqScopedBackingBean.foo()}" value="Submit"/>
    </h:form>
</h:body>
</html>

并且 foo 的实现如下所示:

package biz.tugay.jsftags;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class ReqScopedBackingBean {
    public String foo() {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("message", "Success!!");
        return "hello?faces-redirect=true";
    }
}

最后hello.xhtml如下:

<!DOCTYPE html>
<html xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Hello</title>
</h:head>
<h:body>
    <h:outputText value="#{flash.keep.message}"/>
</h:body>
</html>

在上面的例子中,当我点击提交时,我将被重定向到 hello.xhtml 并看到“成功!!” 文字就好了。当我刷新页面时,我仍然会看到该消息,因为我正在调用 keep 方法,如下所示:

#{flash.keep.message}

现在转到另一个示例:

index.xhtml 和 ReqScopedBackingBean 是一样的,但是这次 hello.xhtml 如下:

<!DOCTYPE html>
<html xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Hello</title>
</h:head>
<h:body>
    #{otherBean.pullValuesFromFlash()}
    <h:outputText value="#{otherBean.message}"/>
</h:body>
</html>

如下OtherBean.java

@ManagedBean
@RequestScoped
public class OtherBean {

    private String message;

    public void pullValuesFromFlash() {
        final Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        flash.keep("message");
        final String message = (String) flash.get("message");
        setMessage(message);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

我期望行为与第一个示例相同。但是,当我在 hello.xhtml 上时,我不会看到成功消息。但是,如果我注释掉该行:

// flash.keep("message");

然后将出现消息(但在 hello.xhtml 上刷新不会像第一个示例中那样重新显示成功消息)。

我的问题是,这里有什么区别?怎么

#{flash.keep.message}

并且不同于

flash.keep("message");
final String message = (String) flash.get("message");

?

4

1 回答 1

1

您遇到了一个 Mojarra 错误Flash#keep()。基本上,Mojarra 不必要地从当前闪存范围中删除条目,然后再将其放入下一个闪存范围。

如果您将逻辑重新排序如下,它将起作用。

public void pullValuesFromFlash() {
    Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
    String message = (String) flash.get("message");
    flash.keep("message");
    setMessage(message);
}

我已根据问题 4167修复了此错误。


与具体问题无关,那些<h:outputText>s 是不必要的。您可以安全地删除它们以减少样板文件。另请参阅是否建议对所有内容使用 h:outputText?

于 2016-06-24T21:19:27.030 回答