我正在开发一个 Struts 2.3 应用程序。在该应用程序中,我从 JSP 调用一个静态实用程序方法,该方法将 HttpServletRequest
对象作为参数并在处理请求后返回一个字符串。当我尝试使用 JSP scriptlet 时,使用适当的参数调用了方法,例如:
<%
String resultStr = com.testapp.util.Utility.getResult(request);
%>
但是当我尝试将 OGNL 用于相同的逻辑时,方法被调用时使用空请求对象作为参数而不是实际的请求对象,例如:
<s:set name="resultStr" var="resultStr"
value="@com.testapp.util.Utility@getResult(request)" />
我还在 OGNL 中尝试了 #request
而不是 request
但在那种情况下方法没有被调用。
最佳答案
将此常量放在配置文件中以启用静态方法访问。
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
使用
<s:set name="resultStr" var="resultStr" value="@com.testapp.util.Utility@getResult()" />
并且在 getResult() {
HttpServletRequest request = ServletActionContext.getRequest();
关于java - 在struts2中使用OGNL未将请求对象传递给静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16815151/