java 使用 Action 数据模型值调用 Struts2 JSP 中的静态方法助手类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13080067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling static method helper class in Struts2 JSP with Action data model value
提问by user1775967
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"/>
.
我是 Struts2 新手。我将 Struts2 与UserItem
Action 中的典型数据模型一起使用。与 Struts 标签一起使用时,数据模型看起来不太好<s:property value="userItem.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.
我想要做的是编写一个静态 util 方法Helper.printNice(Foo)
,该方法采用参数 Foo 并在用户友好的显示中打印出 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"/>)
.
如何在静态方法中使用 Struts 属性标记?像这样的东西
com.helper.Helper.printNice(<s:property value="userItem.foo"/>)
。
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...
原因是我的网络应用程序正在读取供应商填充的数据,在许多列中看起来像这样 ["string1", "string2" , ...]。显然,我不想以这种格式向最终用户显示。辅助方法将使它看起来像 string1 <br> string2<br> 等...
回答by Andrea Ligios
EDIT
编辑
From 2.3.20 and higher, static method access won't work anymore, even if activated in the configuration.
从2.3.20 及更高版本开始,静态方法访问将不再起作用,即使在配置中激活也是如此。
For static methods access you need:
对于静态方法访问,您需要:
in Struts.xml
在 Struts.xml 中
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
in your JSP
在你的 JSP 中
<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.
但正如里斯所指出的,如果不是绝对必要的话,应该避免这种情况,因为这不是最佳实践。
In your specific case, i guess the Object containing ["String1","String2",...] is a List, or a Vector, or something like this.
在您的特定情况下,我猜包含 ["String1","String2",...] 的对象是一个列表,或一个向量,或类似的东西。
Then all you need in your JSP is the <s:iterator>
tag like this:
那么你在你的 JSP 中需要的只是这样的<s:iterator>
标签:
<s:iterator name="yourObjectContainingAListOfString">
<s:property />
<br/>
</s:iterator>
回答by SkyWalker
For Static Method Accessyou must need to addfollowing constantin your struts.xml
file.
对于静态方法访问,您必须需要在文件中添加以下常量struts.xml
。
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Example: struts.xml:
示例: struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="sampleAction" class="vaannila.SampleAction">
<result name="success">/WEB-INF/JSP/sample.jsp</result>
</action>
</package>
</struts>
Then from your JSP you can access it in various ways:
然后从您的 JSP 中,您可以通过多种方式访问它:
Example - 1:
示例 - 1:
<b>Output :</b> <s:property value="@vaannila.SampleAction@getSTR()"/> <br>
Where,
在哪里,
vaannila
= Package Name.SampleAction
= Class Name.getSTR()
= Method Name.
vaannila
= 包名称。SampleAction
= 班级名称。getSTR()
= 方法名称。
Example - 2:
示例 - 2:
<b>Output :</b> <s:property value="@vs@getSTR()"/> <br>
Where,
在哪里,
vs
= Value Stack.getSTR()
= Method Name.
vs
= 价值堆栈。getSTR()
= 方法名称。
Example - 3:
示例 - 3:
<b>Output :</b> <s:property value="%{STR}"/> <br>
where,
在哪里,
STR
=STR
is declared and initialized as Static String with getter and setter method in your Java Class
STR
=STR
在 Java 类中使用 getter 和 setter 方法声明并初始化为静态字符串