0

我不能在我的自定义控件中使用任何 bean 值。例如:

这是我的 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>

面孔-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;
  }
}

豆.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}" />
4

1 回答 1

0

在您的 test.Data 类中,我建议您msg像这样实现 getter:

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());
...

需要此 getter 方法来评估您可能赋予msgJSF 页面中的属性的表达式。

编辑,使用 ValueExpression 而不是弃用的 ValueBinding:

ValueExpression ve = getValueExpression("msg");
if (ve != null) {
    return (String) ve.getValue(getFacesContext().getELContext());
}
于 2009-09-15T15:48:13.117 回答