使用 Action 数据模型值在 Struts2 JSP 中调用静态方法帮助程序类

2023-09-24Java开发问题
1

本文介绍了使用 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>

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

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13