You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by JOSE L MARTINEZ-AVIAL <jl...@gmail.com> on 2012/10/21 09:12:07 UTC

json result type not parse OGNL expressions in params

Hi,
I'm using a json result type defined as follows:

<result-type name="json-action" class="org.apache.struts2.json.JSONResult">
<param name="ignoreHierarchy">false</param>
<param
name="includeProperties">resultName,resultMessage,data.*,fieldErrors.*,actionErrors.*,actionMessages.*</param>
<param name="excludeNullProperties">true</param>
<param name="noCache">true</param>
<param name="errorCode">${errorCode}</param>
</result-type>

Everything works great, except the parameter errorCode. I populated the
errorCode in the ValueStack doing:

invocation.getStack().set("errorCode", errorCode);

where errorCode is an int. But the errorcode is not been setup in the
response, and in fact I can see the following stacktrace:
2012-10-21 02:54:01,060 INFO  com.opensymphony.xwork2.ObjectFactory  -
Exception
Caught OgnlException while setting property 'errorCode' on type
'org.apache.struts2.json.JSONResult'. - Class: ognl.OgnlRuntime
File: OgnlRuntime.java
Method: callAppropriateMethod
Line: 1226 - ognl/OgnlRuntime.java:1226:-1
        at
com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:412)
        at
com.opensymphony.xwork2.ognl.OgnlUtil.setProperty(OgnlUtil.java:160)
        at
com.opensymphony.xwork2.ognl.OgnlReflectionProvider.setProperty(OgnlReflectionProvider.java:91)
        at
com.opensymphony.xwork2.ObjectFactory.buildResult(ObjectFactory.java:243)
        at
com.opensymphony.xwork2.DefaultActionInvocation.createResult(DefaultActionInvocation.java:223)
        at
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:372)
        at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:282)
        at
com.spb.mvc.security.authentication.web.RedirectToLoginInterceptor.intercept(RedirectToLoginInterceptor.java:28)
        at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
        at
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
        at
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
        at
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
        at
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
        at
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
        at
com.spb.eco.security.auditory.web.Log4jSessionIdFilter.doFilter(Log4jSessionIdFilter.java:47)
        at
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
        at
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3496)
        at
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(Unknown Source)
        at
weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
        at
weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
        at
weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
Caused by: java.lang.NoSuchMethodException:
org.apache.struts2.json.JSONResult.setErrorCode(java.lang.String)
        at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1226)
        at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474)
        at
ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
        at
ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
        at
com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
        at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2245)
        at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
        at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
        at ognl.SimpleNode.setValue(SimpleNode.java:301)
        at ognl.Ognl.setValue(Ognl.java:737)
        at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:209)
        at
com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:405)

So it seems it is trying to setup the errorCode with the string
${errorCode}, instead of parsing the OGNL expression. I found a similar
question on StackOverflow
http://stackoverflow.com/questions/4372498/dynamic-wrapprefix-for-json-result-type-in-struts2,
where somebody sees that the jsonResult does not parse the parameters. But
for example the result type httpheader does parse OGNL expression, so is
there any way I can get the json type to parse the parameter value?

Thanjks

Re: json result type not parse OGNL expressions in params

Posted by JOSE L MARTINEZ-AVIAL <jl...@gmail.com>.
I just tried that, but it does not make any difference.

2012/10/21 Per Pascal Grube <cy...@flightmare.net>

> Hi,
>
> On Sunday 21 October 2012 03:12:07 JOSE L MARTINEZ-AVIAL wrote:
> > Hi,
> > I'm using a json result type defined as follows:
> >
> > <result-type name="json-action"
> class="org.apache.struts2.json.JSONResult">
> > <param name="ignoreHierarchy">false</param>
> > <param
> >
> name="includeProperties">resultName,resultMessage,data.*,fieldErrors.*,actio
> > nErrors.*,actionMessages.*</param> <param
> > name="excludeNullProperties">true</param>
> > <param name="noCache">true</param>
> > <param name="errorCode">${errorCode}</param>
> > </result-type>
>
> I don't know if it makes a difference, but I use %{ } in the param tag to
> get
> the values from my actions.
>
> Regards,
>
> Pascal
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: json result type not parse OGNL expressions in params

Posted by Per Pascal Grube <cy...@flightmare.net>.
Hi,

On Sunday 21 October 2012 03:12:07 JOSE L MARTINEZ-AVIAL wrote:
> Hi,
> I'm using a json result type defined as follows:
> 
> <result-type name="json-action" class="org.apache.struts2.json.JSONResult">
> <param name="ignoreHierarchy">false</param>
> <param
> name="includeProperties">resultName,resultMessage,data.*,fieldErrors.*,actio
> nErrors.*,actionMessages.*</param> <param
> name="excludeNullProperties">true</param>
> <param name="noCache">true</param>
> <param name="errorCode">${errorCode}</param>
> </result-type>

I don't know if it makes a difference, but I use %{ } in the param tag to get 
the values from my actions.

Regards,

Pascal

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: json result type not parse OGNL expressions in params

Posted by Lukasz Lenart <lu...@apache.org>.
2012/10/22 JOSE L MARTINEZ-AVIAL <jl...@gmail.com>:
> I just created the issue:
>
> https://issues.apache.org/jira/browse/WW-3906
>
> I think I can implement part of this issue on my own. What do I need to do
> in order to collaborate in this?

Just prepare a patch and attach it to the issue with granting rights to ASF


Regards
-- 
Łukasz
mobile +48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: json result type not parse OGNL expressions in params

Posted by JOSE L MARTINEZ-AVIAL <jl...@gmail.com>.
I just created the issue:

https://issues.apache.org/jira/browse/WW-3906

I think I can implement part of this issue on my own. What do I need to do
in order to collaborate in this?

Thanks

JL


2012/10/22 Lukasz Lenart <lu...@apache.org>

> 2012/10/21 JOSE L MARTINEZ-AVIAL <jl...@gmail.com>:
> > I just reviewed the code, and it seems that the HttpHeaderResult only
> > parses the parameters headers and errorMessage, but not the parameters
> > status or error. I think it could be a nice thing to have. my workaround
> is
> > to extend JSONResult incorporating the following:
> >
> > public void setStatusCode(String statusCode) {
> >         ValueStack stack = ActionContext.getContext().getValueStack();
> >
> >
> setStatusCode(Integer.parseInt(TextParseUtil.translateVariables(statusCode,
> > stack)));
> > }
> >
> > public void setErrorCode(String errorCode) {
> >         ValueStack stack = ActionContext.getContext().getValueStack();
> > setErrorCode(Integer.parseInt(TextParseUtil.translateVariables(errorCode,
> > stack)));
> > }
> >
> > It works fine  but I think that this feature should be incorporated to
> all
> > parameters. I would like to know opinions from other people before
> opening
> > a jira issue for this.
>
> It makes sense, please register an issue.
>
> But jus one thing, you should delay expression evaluation to very end
> and not to evaluate params when you setting them up, rather when you
> using them in a code, like:
>
> public void setErrorCode(String errorCode) {
>      this.errorCode = errorCode;
> }
>
> and in
>
> protected void writeToResponse(HttpServletResponse response, String
> json, boolean gzip) throws IOException {
>     int errorCode = evaluate(errorCode);
>     JSONUtil.writeJSONToResponse(new SerializationParams(response,
> getEncoding(), isWrapWithComments(),
>             json, false, gzip, noCache, statusCode, errorCode, prefix,
> contentType, wrapPrefix,
>             wrapSuffix));
> }
>
>
> Regards
> --
> Łukasz
> mobile +48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: json result type not parse OGNL expressions in params

Posted by Lukasz Lenart <lu...@apache.org>.
2012/10/21 JOSE L MARTINEZ-AVIAL <jl...@gmail.com>:
> I just reviewed the code, and it seems that the HttpHeaderResult only
> parses the parameters headers and errorMessage, but not the parameters
> status or error. I think it could be a nice thing to have. my workaround is
> to extend JSONResult incorporating the following:
>
> public void setStatusCode(String statusCode) {
>         ValueStack stack = ActionContext.getContext().getValueStack();
>
> setStatusCode(Integer.parseInt(TextParseUtil.translateVariables(statusCode,
> stack)));
> }
>
> public void setErrorCode(String errorCode) {
>         ValueStack stack = ActionContext.getContext().getValueStack();
> setErrorCode(Integer.parseInt(TextParseUtil.translateVariables(errorCode,
> stack)));
> }
>
> It works fine  but I think that this feature should be incorporated to all
> parameters. I would like to know opinions from other people before opening
> a jira issue for this.

It makes sense, please register an issue.

But jus one thing, you should delay expression evaluation to very end
and not to evaluate params when you setting them up, rather when you
using them in a code, like:

public void setErrorCode(String errorCode) {
     this.errorCode = errorCode;
}

and in

protected void writeToResponse(HttpServletResponse response, String
json, boolean gzip) throws IOException {
    int errorCode = evaluate(errorCode);
    JSONUtil.writeJSONToResponse(new SerializationParams(response,
getEncoding(), isWrapWithComments(),
            json, false, gzip, noCache, statusCode, errorCode, prefix,
contentType, wrapPrefix,
            wrapSuffix));
}


Regards
-- 
Łukasz
mobile +48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: json result type not parse OGNL expressions in params

Posted by JOSE L MARTINEZ-AVIAL <jl...@gmail.com>.
I just reviewed the code, and it seems that the HttpHeaderResult only
parses the parameters headers and errorMessage, but not the parameters
status or error. I think it could be a nice thing to have. my workaround is
to extend JSONResult incorporating the following:

public void setStatusCode(String statusCode) {
        ValueStack stack = ActionContext.getContext().getValueStack();

setStatusCode(Integer.parseInt(TextParseUtil.translateVariables(statusCode,
stack)));
}

public void setErrorCode(String errorCode) {
        ValueStack stack = ActionContext.getContext().getValueStack();
setErrorCode(Integer.parseInt(TextParseUtil.translateVariables(errorCode,
stack)));
}

It works fine  but I think that this feature should be incorporated to all
parameters. I would like to know opinions from other people before opening
a jira issue for this.

JL

2012/10/21 JOSE L MARTINEZ-AVIAL <jl...@gmail.com>

> Hi,
> I'm using a json result type defined as follows:
>
> <result-type name="json-action" class="org.apache.struts2.json.JSONResult">
>  <param name="ignoreHierarchy">false</param>
> <param
> name="includeProperties">resultName,resultMessage,data.*,fieldErrors.*,actionErrors.*,actionMessages.*</param>
>  <param name="excludeNullProperties">true</param>
> <param name="noCache">true</param>
>  <param name="errorCode">${errorCode}</param>
> </result-type>
>
> Everything works great, except the parameter errorCode. I populated the
> errorCode in the ValueStack doing:
>
> invocation.getStack().set("errorCode", errorCode);
>
> where errorCode is an int. But the errorcode is not been setup in the
> response, and in fact I can see the following stacktrace:
> 2012-10-21 02:54:01,060 INFO  com.opensymphony.xwork2.ObjectFactory  -
> Exception
> Caught OgnlException while setting property 'errorCode' on type
> 'org.apache.struts2.json.JSONResult'. - Class: ognl.OgnlRuntime
> File: OgnlRuntime.java
> Method: callAppropriateMethod
> Line: 1226 - ognl/OgnlRuntime.java:1226:-1
>         at
> com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:412)
>         at
> com.opensymphony.xwork2.ognl.OgnlUtil.setProperty(OgnlUtil.java:160)
>         at
> com.opensymphony.xwork2.ognl.OgnlReflectionProvider.setProperty(OgnlReflectionProvider.java:91)
>         at
> com.opensymphony.xwork2.ObjectFactory.buildResult(ObjectFactory.java:243)
>         at
> com.opensymphony.xwork2.DefaultActionInvocation.createResult(DefaultActionInvocation.java:223)
>         at
> com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:372)
>         at
> com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:282)
>         at
> com.spb.mvc.security.authentication.web.RedirectToLoginInterceptor.intercept(RedirectToLoginInterceptor.java:28)
>         at
> com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
>         at
> org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
>         at
> org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
>         at
> org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
>         at
> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
>         at
> weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
>         at
> com.spb.eco.security.auditory.web.Log4jSessionIdFilter.doFilter(Log4jSessionIdFilter.java:47)
>         at
> weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
>         at
> weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3496)
>         at
> weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
>         at weblogic.security.service.SecurityManager.runAs(Unknown Source)
>         at
> weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
>         at
> weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
>         at
> weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
>         at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
>         at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
> Caused by: java.lang.NoSuchMethodException:
> org.apache.struts2.json.JSONResult.setErrorCode(java.lang.String)
>         at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1226)
>         at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474)
>         at
> ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
>         at
> ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
>         at
> com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
>         at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2245)
>         at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
>         at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
>         at ognl.SimpleNode.setValue(SimpleNode.java:301)
>         at ognl.Ognl.setValue(Ognl.java:737)
>         at
> com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:209)
>         at
> com.opensymphony.xwork2.ognl.OgnlUtil.internalSetProperty(OgnlUtil.java:405)
>
> So it seems it is trying to setup the errorCode with the string
> ${errorCode}, instead of parsing the OGNL expression. I found a similar
> question on StackOverflow
> http://stackoverflow.com/questions/4372498/dynamic-wrapprefix-for-json-result-type-in-struts2,
> where somebody sees that the jsonResult does not parse the parameters. But
> for example the result type httpheader does parse OGNL expression, so is
> there any way I can get the json type to parse the parameter value?
>
> Thanjks
>