4

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

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
user1775967
  • 631
  • 2
  • 6
  • 14
  • 2
    its possible to access a static method using OGNL in Struts2 (so long as you turn it on in your struts.xml and use the @ sign), but you shouldn't really need to and its not a best practice. Why not just provide a pretty print method on your action or model that you can access in your jsp from the value stack in the same way as you would access any other value? – rees Oct 26 '12 at 03:10
  • @rees Seems like an answer to me, not comment. :) – Aleksandr M Oct 26 '12 at 09:24

2 Answers2

7

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>
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank you so much for your advice. So helpful. I was thinking for the helper method because my data model UserItem has many fields of Foo1, Foo2, etc... with the same data formmat. So, I'd call one helper method and pass in the Foo1, Foo2 to do the pretty print. But of course, I'll try your best-practice suggestion as well. – user1775967 Oct 26 '12 at 17:31
  • @user1775967 - you could also use methods on string itself like yourString.replace() in your jsp if your operations are simple. – Arvind Sridharan Sep 03 '13 at 13:04
1

For Static Method Access you must need to add following constant in your struts.xml file.

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

Example: 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:

Example - 1:

<b>Output :</b> <s:property value="@vaannila.SampleAction@getSTR()"/> <br>

Where,

  1. vaannila = Package Name.
  2. SampleAction = Class Name.
  3. getSTR() = Method Name.

Example - 2:

<b>Output :</b> <s:property value="@vs@getSTR()"/> <br>

Where,

  1. vs = Value Stack.
  2. getSTR() = Method Name.

Example - 3:

<b>Output :</b> <s:property value="%{STR}"/> <br>

where,

  1. STR = STR is declared and initialized as Static String with getter and setter method in your Java Class
SkyWalker
  • 28,384
  • 14
  • 74
  • 132