2009-09-15 61 views
0

我不能例如在我的自定義控制使用任何bean值: 使用來自豆類值:不能Facelets中的自定義組件

這是我的foo.taglib.xml文件。

<facelet-taglib> 
    <namespace>http://www.penguenyuvasi.org/tags</namespace> 
    <tag> 
    <tag-name>test</tag-name> 
    <component> 
     <component-type>test.Data</component-type> 
    </component> 
    </tag> 
</facelet-taglib> 

faces-config.xml中

<component> 
    <component-type>test.Data</component-type> 
    <component-class>test.Data</component-class> 
    </component> 

test.Data類

package test; 

import java.io.IOException; 
import javax.faces.component.UIComponentBase; 
import javax.faces.context.FacesContext; 

public class Data extends UIComponentBase { 

    private Object msg; 

    @Override 
    public String getFamily() { 
    return "test.Data"; 
    } 

    @Override 
    public void encodeBegin(FacesContext context) throws IOException { 
    super.encodeBegin(context); 
    context.getResponseWriter().write(msg.toString()); 
    } 

    public void setMsg(Object msg) { 
    this.msg = msg; 
    } 
} 

Bean.java:

package test; 

public class Bean { 

    private String temp = "vada"; 

    public String getTemp() { 
    return temp; 
    } 
} 

test.xhtml(不工作)

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:py="http://www.penguenyuvasi.org/tags"> 
    <py:test msg="#{bean.temp}" /> 
</html> 

test.xhtml(作品)

<py:test msg="#{bean.temp}" /> 

回答

0

在你test.Data類,我建議你實現getter方法msg這樣的:

public String getMsg() { 
    if (msg != null) { 
     return msg; 
    } 
    ValueBinding vb = getValueBinding("msg"); 
    if (vb != null) { 
     return (String) vb.getValue(getFacesContext()); 
    } 
    return null; 
} 

,然後在你的encodeBegin方法:

... 
context.getResponseWriter().write(getMsg()); 
... 

需要該吸氣劑的方法爲了評估你在JSF頁面中給出的msg屬性的表達式。

編輯,使用ValueExpression代替過時的ValueBinding:

ValueExpression ve = getValueExpression("msg"); 
if (ve != null) { 
    return (String) ve.getValue(getFacesContext().getELContext()); 
} 
+0

非常感謝。優秀的解決方 附加信息:不建議使用ValueBinding。應該使用ValueExpression。 – 2009-09-15 16:05:35

+0

是的,的確,必須使用ValueExpression。 – romaintaz 2009-09-15 16:18:03

+0

ValueExpression ve = getValueExpression(「msg」);如果(ve!= null){ } return(String)ve.getValue(getFacesContext()。getELContext()); } – 2009-09-16 05:25:57