JSF, writeAttribute("value", str, null) fails with strings obtained through ValueExpression.getValue

Posted by Roma on Stack Overflow See other posts from Stack Overflow or by Roma
Published on 2009-10-03T05:28:29Z Indexed on 2010/05/06 23:08 UTC
Read the original article Hit count: 235

Filed under:
|

Hello, I'm having a somewhat weird problem with custom JSF component.

Here's my renderer code:

public class Test extends Renderer {

 public void encodeBegin(final FacesContext context,
                        final UIComponent component) throws IOException {

        ResponseWriter writer = context.getResponseWriter();

        writer.startElement("textarea", component);

        String clientId = component.getClientId(context);
        if (clientId != null)
              writer.writeAttribute("name", clientId, null);

        ValueExpression exp = component.getValueExpression("value");
        if (exp != null && exp.getValue(context.getELContext()) != null) {
              String val = (String) exp.getValue(context.getELContext());
              System.out.println("Value: " + val);
              writer.writeAttribute("value", val,  null);
        }

    writer.endElement("textarea");
    writer.flush();
 }
}

The code generates:

<textarea name="j_id2:j_id12:j_id19" value=""></textarea>

Property binding contains "test" and as it should be, "Value: test" is successfully printed to console.

Now, if I change the code to:

public void encodeBegin(final FacesContext context,
                        final UIComponent component) throws IOException {

        ResponseWriter writer = context.getResponseWriter();

        writer.startElement("textarea", component);

        String clientId = component.getClientId(context);
        if (clientId != null)
             writer.writeAttribute("name", clientId, null);

        ValueExpression exp = component.getValueExpression("value");
        if (exp != null && exp.getValue(context.getELContext()) != null) {
             String val = (String) exp.getValue(context.getELContext());
             String str = "new string";
             System.out.println("Value1: " + val + ", Value2: " + str);
             writer.writeAttribute("value", str,  null);
        }

        writer.endElement("textarea");
        writer.flush();
 }

generated html is:

<textarea name="j_id2:j_id12:j_id19" value="new string"></textarea>

and console output is "Value1: test, Value2: new string"

What's going on here? This doesn't make sense. Why would writeAttribute differentiate between the two strings?

Additional info:

  • The component extends UIComponentBase
  • I am using it with facelets

© Stack Overflow or respective owner

Related posts about jsf

Related posts about java