简体   繁体   中英

OGNL Syntax Issue

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM