2016-05-14 20 views
0

jsfページで、私はui:includeを使用しています。
いるindex.xhtmljsfをinclude xhtmlのパラメータとして渡す

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 
    This is from included file.<br/> 
    #{myBean[greeting]} 
</ui:composition> 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 
<h:head> 
    <title>test</title> 
</h:head> 
<h:body> 
    <ui:include src="included.xhtml" > 
    <ui:param name="myBean" value="beanA" /> 
    </ui:include> 
</h:body> 
</html> 

と私は付属のコンテンツは、Beanのメソッド
included.xhtmlを呼び出すことができるようにBean名を渡したいですBeanはできるだけシンプルです
BeanA.java:

stackoverflowの上前の回答を見て3210
package com.test; 

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 

@ManagedBean 
public class BeanA implements Serializable{ 
    public String getGreeting(){ 
    return "hello from BeanA"; 
    } 
} 

JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)? 私は仕事にこれを期待していたが、それはしていません。 「これはインクルードされたファイルからです」テキストが表示されますが、BeanAプロパティは表示されません。誰も私になぜ教えてもらえますか?

最終的に私はインクルードしたいBean名とメソッド名の両方をインクルードしたファイルに渡したいと思っています。しかし、私はこの単純な例が私が期待するように行動することさえできません。

クロサギ科2.2.0、 のApache Tomcat/7.0.55あなたの助けKukeltje & BalusCため

+1

なぜ 'soulあなたが明示的に参照するリンクがELをparam値( '#{beanA'})で使用していますか?そしてどこにもエラーはないと確信していますか? (たとえば、どこかにh:メッセージを含めたり、開発モードでアプリを実行している場合など) – Kukeltje

+0

Kukeltje:を試しましたが、うまくいかなかった。 は出力しません。 tomcatエラーログにもエラー出力はありません。 – rgh

+0

変数 '#{greeting}'はどこで定義しましたか?そのようなものがなく、それがプロパティ名であるならば、あなたは '#{myBean.greeting}'を使うべきです。 – BalusC

答えて

1

感謝。

ここでは、Beanとメソッド名の両方をパラメータとして渡している実例を示しています。同じ問題を持つ人のために、ここにある:

BeanA.java:

package com.test; 

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 

@ManagedBean 
public class BeanA implements Serializable{ 
    public String getHello(){ 
    return "hello from BeanA"; 
    } 
    public String getGoodbye(){ 
    return "goodbye from BeanA"; 
    } 
} 

BeanB.java:

package com.test; 

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 

@ManagedBean 
public class BeanB implements Serializable{ 
    public String getHello(){ 
    return "hello from BeanB"; 
    } 
    public String getGoodbye(){ 
    return "goodbye from BeanB"; 
    } 
} 

いるindex.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 
<h:head> 
    <title>test</title> 
</h:head> 
<h:body> 

    Passing in beanA as a parameter:<br/> 
    <ui:include src="included1.xhtml" > 
    <ui:param name="myBean" value="#{beanA}" /> 
    </ui:include> 
    <hr/> 
    Passing in beanB as a parameter:<br/> 
    <ui:include src="included1.xhtml" > 
    <ui:param name="myBean" value="#{beanB}" /> 
    </ui:include> 
    <hr/> 
    Passing in beanA and method 'hello' as parameters:<br/> 
    <ui:include src="included2.xhtml" > 
    <ui:param name="myBean" value="#{beanA}" /> 
    <ui:param name="myMethod" value="hello" /> 
    </ui:include> 
    <hr/> 
    Passing in beanA and method 'goodbye' as parameters:<br/> 
    <ui:include src="included2.xhtml" > 
    <ui:param name="myBean" value="#{beanA}" /> 
    <ui:param name="myMethod" value="goodbye" /> 
    </ui:include> 
    <hr/> 
    Passing in beanB and method 'hello' as parameters:<br/> 
    <ui:include src="included2.xhtml" > 
    <ui:param name="myBean" value="#{beanB}" /> 
    <ui:param name="myMethod" value="hello" /> 
    </ui:include> 
    <hr/> 
    Passing in beanB and method 'goodbye' as parameters:<br/> 
    <ui:include src="included2.xhtml" > 
    <ui:param name="myBean" value="#{beanB}" /> 
    <ui:param name="myMethod" value="goodbye" /> 
    </ui:include> 
    <h:messages /> 

</h:body> 
</html> 

included1.xhtml:

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 

    #{myBean.hello} 

</ui:composition> 

included2。XHTML:

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 

    #{myBean[myMethod]} 

</ui:composition> 

出力:
パラメータとしてbeanAに渡す:パラメータとしてbeanBに渡すBeanA

から
こんにちは:
こんにちはBeanB
から

パラメータとしてbeanAとメソッド 'hello'を渡す:
hello from BeanA beanBに渡すBeanB
から
ハロー

:beanBおよび方法 'ハロー' パラメータとして渡すBeanA

から
別れ:beanAおよび方法 'さようなら' パラメータとして渡す

メソッド 'goodbye'をパラメータとして使用します。
さよならBeanB

関連する問題