views:

209

answers:

1

In this code:

<html:text property="txtItem5" disabled="disTxtItem5">

I can see that "txtItem5" comes from a getTxtItem5() in the ActionForm, but searching the project for other substrings of "disTxtItem5" seemingly reveals nothing remotely related, though clearly somehow the framework is pulling a boolean from this string, which clearly means that it's more complicated than my current understanding.

Could someone give a good explanation of how these expressions are evaluated, or point me in the direction of one?

+2  A: 

EDIT: In my original response (see below) I said that Struts manages the conversion, but I was wrong. I didn’t remember exactly what was going so I pulled out Struts’s sources and took a look. It turns out that the conversion is made by the server. The JSP is converted to a servlet before execution and it is here that false is used for non boolean values.

For example, I used the following tag:

<html:text property="nr" disabled="BlaBla" />

Which was translated to the following html (no disabled):

<input type="text" name="nr" value="123">

This happened in the servlet. Here is what my servlet contains for the above tag:

//  html:text
    org.apache.struts.taglib.html.TextTag _jspx_th_html_005ftext_005f0 = (org.apache.struts.taglib.html.TextTag) _005fjspx_005ftagPool_005fhtml_005ftext_005fproperty_005fdisabled_005fnobody.get(org.apache.struts.taglib.html.TextTag.class);
    _jspx_th_html_005ftext_005f0.setPageContext(_jspx_page_context);
    _jspx_th_html_005ftext_005f0.setParent((javax.servlet.jsp.tagext.Tag) _jspx_th_html_005fform_005f0);
    _jspx_th_html_005ftext_005f0.setProperty("nr");
    _jspx_th_html_005ftext_005f0.setDisabled(false);
    int _jspx_eval_html_005ftext_005f0 = _jspx_th_html_005ftext_005f0.doStartTag();

As it can be seen, the disabled value is generated with false, directly. I did some more digging into the Jasper compiler (I used Tomcat) and I think that it is the org.apache.jasper.compiler.JspUtil class that is responsible for the conversion, with the following code:

public static boolean booleanValue(String s) {
  boolean b = false;
  if (s != null) {
    if (s.equalsIgnoreCase("yes")) {
       b = true;
    } else {
       b = Boolean.valueOf(s).booleanValue();
    }
  }
  return b;
}

Since I inserted BlaBla in the disabled field this should fallback to Boolean.valueOf(s).booleanValue(); which does the following:

public static Boolean valueOf(String s) {
  return toBoolean(s) ? TRUE : FALSE;
}

private static boolean toBoolean(String name) { 
  return ((name != null) && name.equalsIgnoreCase("true"));
}

This way, BlaBla results in false.

ORIG: The following was my original response, but was incorrect. What I was describing was in fact what happens when request parameters are binded to the action form.

The disabled attribute is of type boolean, so it should receive only values that map to a boolean. disabled="disTxtItem5" would throw a ConversionException because the text disTxtItem5 does not map to a boolean.

Struts uses CommonBeanUtils to make conversions, so a BooleanConverter will be used, with code like the following:

String stringValue = value.toString();
if (stringValue.equalsIgnoreCase("yes") ||
   stringValue.equalsIgnoreCase("y") ||
   stringValue.equalsIgnoreCase("true") ||
   stringValue.equalsIgnoreCase("on") ||
   stringValue.equalsIgnoreCase("1")) {
   return (Boolean.TRUE);
} else if (stringValue.equalsIgnoreCase("no") ||
   stringValue.equalsIgnoreCase("n") ||
   stringValue.equalsIgnoreCase("false") ||
   stringValue.equalsIgnoreCase("off") ||
   stringValue.equalsIgnoreCase("0")) {
   return (Boolean.FALSE);
} else if (useDefault) {
   return (defaultValue);
} else {
   throw new ConversionException(stringValue);
}

At this point I don’t remember if Struts just logs the exception and fails silently setting false as the value of the parameter or, the exception is propagated (it’s been a while since I used Struts :D but I’m more inclined to think that it just sets false and continues).

The logs should point out the exception even if it is ignored. Setting a logger for org.apache.commons.beanutils or org.apache.struts should indicate any conversion errors.

dpb
I had some other questions as to why some things were or weren't working, and this answers all of them. Incredibly helpful, thanks a lot.
T.R.