我是 Struts2 新手。我正在使用带有典型数据模型的 Struts2 UserItem
在一个 Action 中。与 Struts 标签一起使用时,数据模型看起来不太好 <s:property value="userItem.foo"/>
.
我想做的是写一个静态util方法Helper.printNice(Foo)
它采用参数 Foo 并在用户友好的显示中打印出包含在 Foo 中的值。
如何将 Struts 属性标记与静态方法一起使用?像这样的东西
com.helper.Helper.printNice(<s:property value="userItem.foo"/>)
.
这是因为我的网络应用程序正在读取供应商填充的数据,在许多列中看起来像这样 ["string1", "string2", ...]。显然,我不想以这种格式向最终用户显示。辅助方法会使它看起来像 string1
string2
等...
最佳答案
编辑
从 2.3.20 及更高版本,static method access won't work anymore ,即使在配置中激活。
对于静态方法访问你需要:
在 Struts.xml 中
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
在你的 JSP 中
<s:property value="@com.your.full.package.Classname@methodName(optionalParameters)" />
但正如 rees 所指出的,如果不是绝对必要,应该避免这种情况,因为这不是最佳实践。
在您的特定情况下,我猜包含 ["String1","String2",...] 的对象是一个列表、一个 vector 或类似的东西。
那么您在 JSP 中所需要的就是 <s:iterator>
像这样的标签:
<s:iterator name="yourObjectContainingAListOfString">
<s:property />
<br/>
</s:iterator>
关于java - 使用 Action 数据模型值在 Struts2 JSP 中调用静态方法帮助程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13080067/