我如何使用使用 OGNL 访问地图
我正在尝试构建一个时间跟踪页面,在其中显示项目名称、任务和每天计费的小时数。
这是我在 Java 端的对象:
ProjectTO project1 = new ProjectTO();
project1.setProjectName("Project ABC");
TreeMap tasks = new TreeMap();
tasks.put("100_t1", "Requirement");
tasks.put("100_t2", "Development");
project1.setTasks(tasks);
TreeMap hours = new TreeMap();
hours.put("100_t1:Mon", "8");
hours.put("100_t1:Wed", "7");
project1.setHours(hours);
我需要查看该对象以在屏幕上显示值。以下是我在 JSP 页面上的内容:
<s:iterator value="activeProjects">
<tbody>
<tr>
<td><p><s:property value="projectName"/></p></td>
</tr>
<s:iterator value="tasks">
<tr>
<td><s:property value="value"/></td>
<td><s:property value="hours[%{key+':Mon'}]"/></td>
<td><s:property value="hours[%{key+':Tue'}]"/></td>
..........
</tr>
</s:iterator>
</tbody>
</s:iterator>
问题是弄清楚如何显示时间。上面显示的代码片段不起作用。我可以通过使用
并在我的 td 标签中引用它来让它工作 - hours[#星期一])。
有更好的解决方案吗?
编辑:
谢谢戴夫。你是对的。我正在寻找小时["100_t1:Mon"]
。
当我迭代视图层中的对象时,需要根据我正在迭代的任务行的键动态插入“100_t1”。 “周一”、“周二”可以进行硬编码。
我将看看是否可以按照您的建议在视图层之外做得更好。到目前为止,这就是我为我所做的工作。
<s:iterator value="activeProjects">
<tbody>
<tr>
<td><p><s:property value="projectName"/></p></td>
</tr>
<s:iterator value="tasks">
<s:set var="mon" value="%{key+':Mon'}"/>
<s:set var="tue" value="%{key+':Tue'}"/>
<s:set var="wed" value="%{key+':Wed'}"/>
<s:set var="thu" value="%{key+':Thu'}"/>
<s:set var="fri" value="%{key+':Fri'}"/>
<s:set var="sat" value="%{key+':Sat'}"/>
<s:set var="sun" value="%{key+':Sun'}"/>
<tr>
<td><s:property value="value"/></td>
<td><s:property value="hours[#mon]"/></td>
<td><s:property value="hours[#tue]"/></td>
<td><s:property value="hours[#wed]"/></td>
<td><s:property value="hours[#thu]"/></td>
<td><s:property value="hours[#fri]"/></td>
<td><s:property value="hours[#sat]"/></td>
<td><s:property value="hours[#sun]"/></td>
</tr>
</s:iterator>
</s:iterator>
最终编辑:
以下语句帮助我避免使用 s:set 标签。我应该将 hours[key + ':Mon']
包围在 %{}
内。
value="%{hours[key + ':Mon']}"/>
这也有效
value="hours[key + ':Wed']"
I am trying to build a time tracking page where I display the project name, tasks and the hours billed for each day.
Here is the object I have on the Java side:
ProjectTO project1 = new ProjectTO();
project1.setProjectName("Project ABC");
TreeMap tasks = new TreeMap();
tasks.put("100_t1", "Requirement");
tasks.put("100_t2", "Development");
project1.setTasks(tasks);
TreeMap hours = new TreeMap();
hours.put("100_t1:Mon", "8");
hours.put("100_t1:Wed", "7");
project1.setHours(hours);
I'll need to look through this object to display values on the screen. Here is what I have on the JSP Page:
<s:iterator value="activeProjects">
<tbody>
<tr>
<td><p><s:property value="projectName"/></p></td>
</tr>
<s:iterator value="tasks">
<tr>
<td><s:property value="value"/></td>
<td><s:property value="hours[%{key+':Mon'}]"/></td>
<td><s:property value="hours[%{key+':Tue'}]"/></td>
..........
</tr>
</s:iterator>
</tbody>
</s:iterator>
The problem is figuring out how to display the hours. The code snippet shown above does not work. I can get it to work by using <s:set var="mon" value="%{key+':Mon'}">
and referencing it in my td tags - hours[#mon]).
Is there a better solution?
Edit:
Thanks Dave. You are right. I am looking for hours["100_t1:Mon"]
.
When I am iterating over the object in the view layer, the '100_t1' will need to be dynamically inserted based on the key of the task row i am iterating over. 'Mon', 'Tue' can be hardcoded.
I am going to see if I can do this better outside of the View layer as you recommended. As of now this is what I have working for me.
<s:iterator value="activeProjects">
<tbody>
<tr>
<td><p><s:property value="projectName"/></p></td>
</tr>
<s:iterator value="tasks">
<s:set var="mon" value="%{key+':Mon'}"/>
<s:set var="tue" value="%{key+':Tue'}"/>
<s:set var="wed" value="%{key+':Wed'}"/>
<s:set var="thu" value="%{key+':Thu'}"/>
<s:set var="fri" value="%{key+':Fri'}"/>
<s:set var="sat" value="%{key+':Sat'}"/>
<s:set var="sun" value="%{key+':Sun'}"/>
<tr>
<td><s:property value="value"/></td>
<td><s:property value="hours[#mon]"/></td>
<td><s:property value="hours[#tue]"/></td>
<td><s:property value="hours[#wed]"/></td>
<td><s:property value="hours[#thu]"/></td>
<td><s:property value="hours[#fri]"/></td>
<td><s:property value="hours[#sat]"/></td>
<td><s:property value="hours[#sun]"/></td>
</tr>
</s:iterator>
</s:iterator>
Final Edit:
The following statement helps me avoid having to use s:set tags. I should have surrounded hours[key + ':Mon']
within %{}
.
value="%{hours[key + ':Mon']}"/>
also this works too
value="hours[key + ':Wed']"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在短路您真正想要的 OGNL 评估。
您正在寻找的完整 OGNL 表达式实际上是
hours["100_t1:Mon"]
:(IMO)您应该将时间更具体地附加到任务上;事实上,您通过使用映射和临时关系来颠覆 OOP 范例。
此外,如果您确切地知道一周有多少天,请向 JSP 提供每天的工作时间列表。这避免了在视图层中进行测试更困难的工作。
如果您选择不遵循 OOP/MVC 规范,您可以消除一些冗余,如下所示:
再次,我强烈地感觉到这是进行此类工作的错误地方。
You're short-circuiting the OGNL evaluation you actually want.
The full OGNL expression you're looking for is actually
hours["100_t1:Mon"]
:(IMO) you should be attach the hours to a task more concretely; as it is you're kind of subverting OOP paradigms by using maps and ad-hoc relationships.
Also, you know precisely how many days there are in a week, present the JSP with a list of daily hours. This avoids doing work in the view layer where testing is more difficult.
If you choose to not follow OOP/MVC norms, you can eliminate some redundancy like this:
Again, I feel pretty strongly this is the wrong place to do this type of work.