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