这是我的问题:
我在JSP中有一个JavaScript函数,如下所示:
<script type="text/javascript">
function generateTable()
{
var temp = '';
temp = temp + '<logic:iterate name="dataList" id="dto" indexId="dtoIndex" >';
temp = temp + '<logic:equal name="dtoIndex" value="0">';
temp = temp + '<thead>';
temp = temp + '<tr class="topexpression7"></tr></thead><tbody></logic:equal>';
temp = temp + '<tr>';
var propertyArray = new Array('"title"','"jDate"','"employeeId"','"employeeName"');
var arrayLength = propertyArray.length;
var html = '';
var i=0;
for (i=0; i<arrayLength; i++)
{
if (i == 2)
{
// left
html = html + '<logic:present name="dto" property=' + propertyArray[i] + '><td class="left"> <bean:write name="dto" property=' + propertyArray[i] + '/></td></logic:present>';
}
else if (i == 3)
{
// Only applies to this property
html = html + '<logic:present name="dto" property="employeeName">';
html = html + '<td class="left" style="white-space:nowrap;"> ';
html = html + '<nobr><bean:write name="dto" property="employeeName"/>';
html = html + '</nobr></td></logic:present>';
}
else
{
// center
html = html + '<logic:present name="dto" property=' + propertyArray[i] + '><td class="center"> <bean:write name="dto" property=' + propertyArray[i] + '/></td></logic:present>';
}
}
temp = temp + html + '</logic:iterate></tbody>';
// Write out the HTML
document.writeln(temp);
}
</script>
如果我将属性硬编码为where(
i == 3
),那么它将正常工作。呈现预期的效果。但是通过尝试动态解析字符串(其中
i <> 3
),字符串var "html"
每次都是null
。诚然,我的JavaScript充其量只是平均水平。我敢肯定,这是一个简单的解决方法,但是我不敢理解!附言关于我为什么要走这条路的长话,我将为您保留这个故事(不客气)。我只想知道为什么变量
propertyArray[i]
无法正常工作。 最佳答案
JSP是在服务器上呈现的,而JavaScript是在客户端浏览器上呈现的,但是要正确呈现JSP标记,应使其格式正确,即具有有效值的所有必要属性,begin和close标记等。但并非所有标记都是有效的。首先,您的JSP是在服务器上编译的,它无法处理错误的JSP标记。当i != 3
时,您将得到不良的JSP标记。编译JSP时,JavaScript代码像内容一样使用,它对JSP编译器的意义较小,因为它正在查找与JSP语法相对应的标记。用JSP编译器的眼睛看,您会看到logic:present
标记具有属性property
但没有值,因为propertyArray[i]
未被评估为JSP表达式,它只是打破了标记边界。因此,标记未正确编译。如果将JSP标记放入JavaScript代码中,请确保它们一致。