使用Action数据模型值在Struts2 JSP中调用静态方法helper类

use*_*967 4 java static jsp struts2 ognl

我是Struts2的新手.我正在使用Struts2和UserItemAction中的典型数据模型.使用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 <br> string2 <br>等...

And*_*ios 7

编辑

2.3.20及更高版本开始,静态方法访问将不再起作用,即使在配置中激活也是如此.


对于静态方法访问,您需要:

在Struts.xml中

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Run Code Online (Sandbox Code Playgroud)

在你的JSP中

<s:property value="@com.your.full.package.Classname@methodName(optionalParameters)" />
Run Code Online (Sandbox Code Playgroud)

但正如里斯指出的那样,如果不是绝对必要的话,应该避免这种情况,因为这不是最好的做法.

在您的特定情况下,我猜包含["String1","String2",...]的Object是List,Vector或类似的东西.

然后你在JSP中需要的就是这样的<s:iterator>标签:

<s:iterator name="yourObjectContainingAListOfString">
   <s:property /> 
   <br/>
</s:iterator>
Run Code Online (Sandbox Code Playgroud)