2016-06-24 3 views
2

だから私はこの非常に小さなJSFの例があります。キーがBeanにあり、FaceletsページのELにない場合、キーを削除するflash.keep()はなぜですか?

<!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> 

上記の例では、Submitを押すとhello.xhtmlにリダイレクトされ、 "Success !!"テキストは大丈夫です。

#{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"); 
final String message = (String) flash.get("message"); 

より

#{flash.keep.message} 

と違うのですか?

答えて

1

あなたはFlash#keep()のモハラのバグをヒットしました。基本的には、Mojarraは次のフラッシュスコープに入れる前に、現在のフラッシュ範囲からの入力をremovesに不必要にします。

ロジックを次のように並べ替えると機能します。

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

は私がfixedissue 4167あたりとしてこのバグを持っています。具体的な問題へ


無関係、それら<h:outputText> sが不要です。ボイラープレートを減らすためにそれらを安全に取り外すことができます。参照:Is it suggested to use h:outputText for everything?

関連する問題