tags:

views:

312

answers:

1

Hi, i cannot use any bean value in my custom control.: for instance:

this is my foo.taglib.xml file.

<facelet-taglib>
  <namespace>http://www.penguenyuvasi.org/tags&lt;/namespace&gt;
  <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 class

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 (doesn't work)

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

test.xhtml (works)

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

In your test.Data class, I suggest that you implement the getter for msg like that:

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

And then, in your encodeBegin method:

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

This getter method is needed in order to evaluate the expression you may give to the msg attribute in your JSF page.

Edit, to use ValueExpression instead of the deprecated ValueBinding:

ValueExpression ve = getValueExpression("msg");
if (ve != null) {
    return (String) ve.getValue(getFacesContext().getELContext());
}
romaintaz
Thanks a lot. Excellent solution.Additional info: ValueBinding is deprecated. ValueExpression should be used.
Fırat KÜÇÜK
Yes, indeed, ValueExpression must be used instead.
romaintaz
ValueExpression ve = getValueExpression("msg"); if (ve != null) { return (String) ve.getValue(getFacesContext().getELContext()); }
Fırat KÜÇÜK
If this solution resolved your problem, please accept it (click on the check icon in front of the answer).
romaintaz