OGNL Syntax Issue

主宰稳场 提交于 2019-12-01 08:22:49

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/16288274/ognl-syntax-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!