2009-09-15 38 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