2

I have a Struts 2 JSP page with the following snippet:

<s:property value="%{myVariable}" />

which correctly prints out the value of myVariable.

Now, I want to pass myVariable to a method in my action that computes a result based on the value of myVariable. I tried the following:

<s:property value="%{myMethod(myVariable)}" />

The first line in myMethod prints out a debug statement. With the above snippet, this debug statement was not printed.

I then tried this:

<s:property value="%{myMethod(#myVariable)}" />

My debug statement printed, but the value of myVariable was passed as null even though it has a value when it is printed via <s:property value="%{myVariable}" />

What is the correct syntax for passing a page variable to a Struts 2 method?

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Please post your jsp file. – Ravi K Thapliyal Apr 29 '13 at 22:58
  • If this is not working that's strange. I've seen some strange things if you've used a custom tags. That aside, you should be able to use s:set to assign the variable to a new name and change the scope from page to action. You could also try to to force the scope #attr.myVariable (will search page scope first) – Quaternion Apr 30 '13 at 05:57
  • Which type is your `myVariable` and what type takes your method? – Aleksandr M Apr 30 '13 at 08:13
  • @AleksandrM - The actual type of myVariable is a complex data type from my application, but I've tested with a String variable and encountered the same issue. – SQL Question Apr 30 '13 at 12:52

2 Answers2

0

I think you are missing target object, ex

<s:property value="%{myTarget.myMethod(myVariable)}" />

More: How to pass parameter to method call in Struts 2 OGNL

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • The method that I'm trying to call is inside my Action. It isn't a property of the myVariable object. In my action, I have execute() and a myMethod(String parameter) method. – SQL Question Apr 29 '13 at 21:14
  • So I'm not particularly sure what you mean by myTarget. Is that the fully qualified path to my struts action (i.e. - com.web.actions.ViewDataAction)? – SQL Question Apr 29 '13 at 21:15
0
<s:property value="%{myMethod(myVariable)}" />

is a correct syntax, but to get the value of the method that have a signature

public String myMethod(String value){
  return value;
}

required the getter for myVariable

public String getMyVariable() {
  return myVariable;
}

if you set the value to myVariable like

private String myVariable = "myValue";

then it should be printed in JSP. If the argument is other type it will be converted to String and the method will call.

Roman C
  • 49,761
  • 33
  • 66
  • 176