JSP中<c:set>和el表达式搭配使用中出错

java代码:
Map<String,String> user = new HashMap<String,String>();
user.put("ID1","VALUE1");
user.put("ID2","VALUE2");
user.put("ID3","VALUE3");
model.addAttribute("userInfo",user);

jsp代码1:
<c:set var="result" value="<%=list.get(0).getdataSource()%>"> ---从DB取到的一个字符串:userInfo
<c:set var="index" value="<%=param.get(0).getName()%>">------String类型key 值:ID2
<input type="text" value="${result[index] }">

jsp代码2:
<c:set var="index" value="<%=param.get(0).getName()%>">------String类型key 值:ID2
<input type="text" value="${userInfo[index] }">

代码1的结果:
出现下面异常【Property 'ID2' not found on type java.lang.String】
怎么处理才能正常取到结果[VALUE2]呢?

代码2的结果:
[VALUE2]能正常获取到。

异常原因:
result = "userInfo";
<input type="text" value="${result[index] }">

——><input type="text" value="${"userInfo"[index] }">
注意到userInfo 两边的双引号了吗,其实就是 在 “userInfo” 这个字符串中 找 索引为 “ID2” 的值,当然会报错了。
解决方法还不清楚,可以参考这个:
window.onload = function(){
var array = [1,2,3,4];
var a = "array";
alert(eval(a)[2]);
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答