使用Action数据模型值在Struts2 JSP中调用静态方法helper类 [英] Calling static method helper class in Struts2 JSP with Action data model value

查看:126
本文介绍了使用Action数据模型值在Struts2 JSP中调用静态方法helper类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Struts2的新手。我在一个Action中使用Struts2和典型的datamodel UserItem 。使用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< br>等...

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"/>

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

但正如里斯指出的那样,如果不是非常必要,应该避免这种情况,因为它不是最好的练习。

But as pointed out by rees, this should be avoided if not strictly necessary, because it's not a best practice.

在你的具体情况下,我想包含[String1,String2,...]的对象是一个List,或一个Vector,或者什么的像这样。

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>

这篇关于使用Action数据模型值在Struts2 JSP中调用静态方法helper类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆