使用 Action 数据模型值在 Struts2 JSP 中调用静态方法帮助程序类
我是 Struts2 新手.我在 Action 中使用带有典型数据模型 UserItem
的 Struts2.与 Struts 标签 <s:property value="userItem.foo"/>
一起使用时,数据模型看起来不太好.
I'm a Struts2 newbie. I'm using Struts2 with the typical datamodel UserItem
inside an Action. The datamodel doesn't look good when using with the Struts tag <s:property value="userItem.foo"/>
.
我想要做的是编写一个静态 util 方法 Helper.printNice(Foo)
,它接受参数 Foo 并在用户友好的显示中打印出 Foo 中包含的值.
What I want to do is write a static util method Helper.printNice(Foo)
that takes parameter Foo and prints out the value contained in Foo in a user-friendly display.
如何在静态方法中使用 Struts 属性标记?像这样的东西com.helper.Helper.printNice(<s:property value="userItem.foo"/>)
.
How do I use the Struts property tag with the static method? Something like this
com.helper.Helper.printNice(<s:property value="userItem.foo"/>)
.
原因是我的网络应用程序正在读取供应商填充的数据,在许多列中看起来像这样 ["string1", "string2" , ...].显然,我不想以这种格式向最终用户显示.辅助方法将使它看起来像 string1 <br>string2
等...
The reason for this is my web app is reading data populated by a vendor, which looks like this ["string1", "string2" , ...] in many columns. Obviously, I don't want to display in this format to the end user. The helper method would make it look like string1 <br> string2<br>, etc...
推荐答案
编辑
从 2.3.20 及更高版本开始,静态方法访问将不再起作用,即使在配置中激活.
From 2.3.20 and higher, static method access won't work anymore, even if activated in the configuration.
对于您需要的静态方法访问:
For static methods access you need:
在 Struts.xml 中
in Struts.xml
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
在你的 JSP 中
<s:property value="@com.your.full.package.Classname@methodName(optionalParameters)" />
但正如 rees 所指出的,如果不是绝对必要的话,应该避免这样做,因为这不是最佳实践.
But as pointed out by rees, this should be avoided if not strictly necessary, because it's not a best practice.
在你的具体情况下,我猜包含 ["String1","String2",...] 的对象是一个列表,或者一个向量,或者类似的东西.
In your specific case, i guess the Object containing ["String1","String2",...] is a List, or a Vector, or something like this.
那么您在 JSP 中所需要的就是 <s:iterator>
标记,如下所示:
Then all you need in your JSP is the <s:iterator>
tag like this:
<s:iterator name="yourObjectContainingAListOfString">
<s:property />
<br/>
</s:iterator>
相关文章