Struts 标签属性如何工作?

发布于 2024-08-19 21:41:31 字数 281 浏览 3 评论 0原文

在这段代码中:

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

我可以看到“txtItem5”来自 ActionForm 中的 getTxtItem5(),但是在项目中搜索“disTxtItem5”的其他子字符串似乎没有揭示任何远程相关的内容,尽管显然框架以某种方式从该字符串中提取布尔值,这显然意味着它比我目前的理解更复杂。

有人可以很好地解释如何评估这些表达式,或者为我指出一个方向吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2024-08-26 21:41:31

编辑:在我最初的回复中(见下文)我说Struts 管理转换,但我错了。我不记得到底发生了什么,所以我拿出了 Struts 的源代码并看了一下。原来是服务器进行了转换。 JSP 在执行之前被转换为 servlet,并且此处 false 用于非布尔值。

例如,我使用了以下标记:

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

已翻译为以下 html(未禁用):

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

这发生在 servlet 中。以下是我的 servlet 包含的上述标记内容:

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

可以看出,禁用值是直接使用 false 生成的。我对 Jasper 编译器(我使用 Tomcat)做了一些深入研究,我认为是 org.apache.jasper.compiler.JspUtil 类负责转换,代码如下:

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

由于我在禁用中插入了 BlaBla该字段应该回退到 Boolean.valueOf(s).booleanValue(); ,它执行以下操作:

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

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

这样,BlaBla 结果为 false。

ORIG: 以下是我最初的回复,但不正确。我所描述的实际上是当请求参数绑定到操作表单时发生的情况。disabled

属性是布尔类型,因此它应该只接收映射到布尔值的值。 disabled="disTxtItem5" 会抛出 ConversionException,因为文本 disTxtItem5 未映射到布尔值。

Struts 使用 CommonBeanUtils 进行转换,因此将使用 BooleanConverter,代码如下

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);
}

:关于这一点,我不记得 Struts 是否只是记录异常并失败,默默地将 false 设置为参数值,或者传播异常(自从我使用 Struts 以来已经有一段时间了:D,但我更倾向于认为它只是设置为 false 并继续)。

即使被忽略,日志也应该指出异常。为 org.apache.commons.beanutilsorg.apache.struts 设置记录器应该指示任何转换错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文