Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

java - Calling static method helper class in Struts2 JSP with Action data model value

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

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.

How do I use the Struts property tag with the static method? Something like this 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

EDIT

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:

in Struts.xml

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

in your 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.

Then all you need in your JSP is the <s:iterator> tag like this:

<s:iterator name="yourObjectContainingAListOfString">
   <s:property /> 
   <br/>
</s:iterator>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...