Jsp 在struts2中对象名称为变量时访问对象属性
当属性名为变量时,访问对象列表的特定对象属性时遇到一些问题 这是一个示例,其中users是用户列表,userAttributeList是包含用户属性名称的字符串列表Jsp 在struts2中对象名称为变量时访问对象属性,jsp,struts2,ognl,Jsp,Struts2,Ognl,当属性名为变量时,访问对象列表的特定对象属性时遇到一些问题 这是一个示例,其中users是用户列表,userAttributeList是包含用户属性名称的字符串列表 <% ....... ActionContext.getContext().put("userAttributeList", request.getAttribute("userAttributeList")); %> <s:iterator value="users"> <
<%
.......
ActionContext.getContext().put("userAttributeList", request.getAttribute("userAttributeList"));
%>
<s:iterator value="users">
<s:iterator value="%{userAttributeList}" var="userAttribute">
... <s:property value="%{#userAttribute}" />
</s:iterator>
</s:iterator>
userAttributeList ArrayList包含用户属性的名称。此ArrayList用于简化实现并为此创建动态解决方案:
<s:iterator value="users">
<s:property value="%{username}" />
<s:property value="%{birthDate}" />
<s:property value="%{password}" />
....
</s:iterator>
您只填充了用户对象属性的名称。但没有把价值观。如果希望拥有用户对象的动态字段,则应该使用映射而不是ArrayList
Map<String, Object> userAttributeMap= new HashMap<>();
userAttributeMap.put("username", "John");
userAttributeMap.put("birthDate", "20-05-1990");
....
现在在JSP中,您应该使用OGNL计算映射键
<s:iterator value="%{userAttributeMap}" var="userAttribute">
... <s:property value="%{#userAttribute['username']}" />
<s:property value="%{#userAttribute['birthDate']}" />
</s:iterator>
在你填写的地方贴上代码userAttributeList@Roman我编辑了我的帖子
<s:iterator value="%{userAttributeMap}" var="userAttribute">
... <s:property value="%{#userAttribute['username']}" />
<s:property value="%{#userAttribute['birthDate']}" />
</s:iterator>