问题:下面一段jsp代码会在浏览器上输出什么?
<%
String str = "17D";
session.setAttribute("str", str);
%>
<logic:equal name="str" value="17D">17D</logic:equal>
<logic:equal name="str" value="17F">17F</logic:equal>
答案:17D 17F。
解释:struts的taglib <logic:equal 在进行是否相等比较的时候,并不是把name对应的值(variable)和value按照String进行比较,而是按照以下顺序:
1)判断value是否可以parse成double,如果可以,就按照基本数据类型double来进行比较;否则,转2)
2)判断value是否可以parse成long,如果可以,就按照基本数据类型long来进行比较;否则转3)
3)按照String的equal进行比较。
所以,17D和17F都满足第一个判断,转换成double:17.0进行比较,得出结论:17D=17F。
这是struts 的tag <logic:equal 的一个bug。
解决问题的方法很多,这里提供一个简单但实用的方法(当然不是最好的):将17D转换成17DD,将17F转换成17FF。
转换后的代码如下:
<%
String str = "17D";
if ("17D".equals(str))
{
str = "17DD";
}
if ("17F".equals(str))
{
str = "17FF";
}
session.setAttribute("str", str);
%>
<logic:equal name="str" value="17DD">17D</logic:equal>
<logic:equal name="str" value="17FF">17F</logic:equal>
附Struts的<logic:equal 部分源码:
/**
* We will do a double/float comparison.
*/
protected static final int DOUBLE_COMPARE = 0;
/**
* We will do a long/int comparison.
*/
protected static final int LONG_COMPARE = 1;
/**
* We will do a String comparison.
*/
protected static final int STRING_COMPARE = 2;
/**
* Evaluate the condition that is being tested by this particular tag,
* and return <code>true</code> if the nested body content of this tag
* should be evaluated, or <code>false</code> if it should be skipped.
* This method must be implemented by concrete subclasses.
*
* @param desired1 First desired value for a true result (-1, 0, +1)
* @param desired2 Second desired value for a true result (-1, 0, +1)
*
* @exception JspException if a JSP exception occurs
*/
protected boolean condition(int desired1, int desired2)
throws JspException {
// Acquire the value and determine the test type
int type = -1;
double doubleValue = 0.0;
long longValue = 0;
if ((type < 0) && (value.length() > 0)) {
try {
doubleValue = Double.parseDouble(value);
type = DOUBLE_COMPARE;
} catch (NumberFormatException e) {
;
}
}
if ((type < 0) && (value.length() > 0)) {
try {
longValue = Long.parseLong(value);
type = LONG_COMPARE;
} catch (NumberFormatException e) {
;
}
}
if (type < 0) {
type = STRING_COMPARE;
}
// Acquire the unconverted variable value
Object variable = null;
if (cookie != null) {
Cookie cookies[] =
((HttpServletRequest) pageContext.getRequest()).
getCookies();
if (cookies == null)
cookies = new Cookie[0];
for (int i = 0; i < cookies.length; i++) {
if (cookie.equals(cookies[i].getName())) {
variable = cookies[i].getValue();
break;
}
}
} else if (header != null) {
variable =
((HttpServletRequest) pageContext.getRequest()).
getHeader(header);
} else if (name != null) {
Object bean = TagUtils.getInstance().lookup(pageContext, name, scope);
if (property != null) {
if (bean == null) {
JspException e = new JspException
(messages.getMessage("logic.bean", name));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
try {
variable = PropertyUtils.getProperty(bean, property);
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null)
t = e;
TagUtils.getInstance().saveException(pageContext, t);
throw new JspException
(messages.getMessage("logic.property", name, property,
t.toString()));
} catch (Throwable t) {
TagUtils.getInstance().saveException(pageContext, t);
throw new JspException
(messages.getMessage("logic.property", name, property,
t.toString()));
}
} else {
variable = bean;
}
} else if (parameter != null) {
variable =
pageContext.getRequest().getParameter(parameter);
} else {
JspException e = new JspException
(messages.getMessage("logic.selector"));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
if (variable == null) {
variable = ""; // Coerce null to a zero-length String
}
// Perform the appropriate comparison
int result = 0;
if (type == DOUBLE_COMPARE) {
try {
double doubleVariable =
Double.parseDouble(variable.toString());
if (doubleVariable < doubleValue)
result = -1;
else if (doubleVariable > doubleValue)
result = +1;
} catch (NumberFormatException e) {
result = variable.toString().compareTo(value);
}
} else if (type == LONG_COMPARE) {
try {
long longVariable = Long.parseLong(variable.toString());
if (longVariable < longValue)
result = -1;
else if (longVariable > longValue)
result = +1;
} catch (NumberFormatException e) {
result = variable.toString().compareTo(value);
}
} else {
result = variable.toString().compareTo(value);
}
// Normalize the result
if (result < 0)
result = -1;
else if (result > 0)
result = +1;
// Return true if the result matches either desired value
return ((result == desired1) || (result == desired2));
}