问题描述
当索引是变量时,我在访问列表的特定元素时遇到一些麻烦.
I'm having some trouble accessing a specific element of a list when the index is a variable.
当索引只是一个数字时,我完全不会显示要查找的内容.
When the index is just a number, I have no issues at all displaying what I'm looking for.
<s:property value="#session.userList[1].email" />
效果很好,并显示在UserList的该元素中找到的电子邮件
That works perfectly, and displays the email found in that element of UserList
但是,当我将索引更改为变量时,我很难找到编写ognl语句的正确方法.我尝试了所有可以想到的%#组合,但都没有运气.
However, when I change the index to be a variable, I'm having difficulties finding the proper way to write the ognl statement. I've tried every combination of %# I can think of with no luck.
<s:set var="userIndex" >${param.index}</s:set>
<s:property value="#session.userList[#userIndex].email" />
我应该如何精确格式化ognl语句?
How exactly should I format my ognl statement?
推荐答案
您应在此示例中设置其格式:
You should format it like in this example:
<s:set var="userIndex" value="%{@java.lang.Integer@valueOf(#parameters.index)}" />
<s:property value="%{#session.userList[#userIndex].email}" />
如果您正在使用struts标记,最好使用OGNL代替EL来求值表达式.强制OGNL评估整个表达而不是一部分.
If you are working with the struts tags better use OGNL for evaluate expressions instead of EL. Force OGNL to evaluate whole expression not a part.
需要将变量的值转换为整数类型.用于名称的字符串类型.
Needed to convert to integer type the value of the variable. String types used for names.
这篇关于当索引是变量时,Struts2访问列表的特定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!