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

I have a somewhat strange problem with a custom JSF component.

Here's my render 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>

      

The property binding contains "test" and, as it should, "Value: test" prints successfully to the console.

Now if I changed 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:

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

      

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

What's going on here? This makes no sense. Why does writeAttribute distinguish between two strings?

Additional Information:

  • Component extends UIComponentBase
  • I am using it with facelets
+2


source to share


1 answer


The HTML textarea element has no "value" attribute. In fact, the body of an element is a value. You should get

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

      



Check out the source code for the JSF TextAreaRenderer source code for more tips.

+1


source







All Articles