[
https://issues.apache.org/jira/browse/WW-4846?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16147697#comment-16147697
]
Yasser Zamani edited comment on WW-4846 at 8/30/17 6:03 PM:
------------------------------------------------------------
I think you can try something like below example (posting {{
\{"request":\{"repositoryPath": \[ 'VZWEB:/VZMOBILE/1.jpg' \] \}\} }} *as
string* to your action which has JSON interceptor):
JSP PAGE:
{code:xml}
<%@taglib prefix="s" uri="/struts-tags"%>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>WW-4176</title>
</head>
<body>
<form action="" id="testForm1">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}"/>
testForm1<input type="submit"/>
</form>
<script>
$("#testForm1").submit(function(event){
event.preventDefault(); //Cancel the default action (navigation) of the
click.
var token = $("input[name='_csrf']").val();
var data={"request":{"repositoryPath": [ 'VZWEB:/VZMOBILE/1.jpg' ] }};
$.ajax({
type: "POST",
url: "<s:url namespace="/" action="testMap"/>",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
headers: {
'X-CSRF-TOKEN':token
},
success: function(json) {
alert("success");
},
error: function (xhr, textStatus, errorThrown) {
alert(xhr.responseText);
}
});
});
</script>
</body>
</html>
{code}
JSON Action config:
{code:xml}
<package name="default" namespace="/" extends="struts-default,json-default">
<action name="testMap" method="testMap"
class="me.zamani.yasser.ww_convention.actions.JSON_DropDownPropertyValuesAction">
<interceptor-ref name="json"></interceptor-ref>
<result name="success" type="json">
<param name="noCache">true</param>
</result>
</action>
</package>
{code}
was (Author: yasser.zamani):
I think you can try something like below example (posting
{{\{"request":\{"repositoryPath": [ 'VZWEB:/VZMOBILE/1.jpg' ] \}\}}} to your
action which has JSON interceptor):
JSP PAGE:
{code:xml}
<%@taglib prefix="s" uri="/struts-tags"%>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>WW-4176</title>
</head>
<body>
<form action="" id="testForm1">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}"/>
testForm1<input type="submit"/>
</form>
<script>
$("#testForm1").submit(function(event){
event.preventDefault(); //Cancel the default action (navigation) of the
click.
var token = $("input[name='_csrf']").val();
var data={"request":{"repositoryPath": [ 'VZWEB:/VZMOBILE/1.jpg' ] }};
$.ajax({
type: "POST",
url: "<s:url namespace="/" action="testMap"/>",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
headers: {
'X-CSRF-TOKEN':token
},
success: function(json) {
alert("success");
},
error: function (xhr, textStatus, errorThrown) {
alert(xhr.responseText);
}
});
});
</script>
</body>
</html>
{code}
JSON Action config:
{code:xml}
<package name="default" namespace="/" extends="struts-default,json-default">
<action name="testMap" method="testMap"
class="me.zamani.yasser.ww_convention.actions.JSON_DropDownPropertyValuesAction">
<interceptor-ref name="json"></interceptor-ref>
<result name="success" type="json">
<param name="noCache">true</param>
</result>
</action>
</package>
{code}
> Not able to convert Spring object to the JSON response
> ------------------------------------------------------
>
> Key: WW-4846
> URL: https://issues.apache.org/jira/browse/WW-4846
> Project: Struts 2
> Issue Type: Bug
> Components: Core Results, Plugin - JSON
> Affects Versions: 2.5.13
> Reporter: Vijay Srivastava
> Priority: Blocker
>
> Hi,
> I am using latest version of struts struts-2.5.12 and and spring 4.1 as
> advised. I am having an issue in JSON response. There is an action class
> which is returning Spring request scoped object as JSON response however I am
> getting error as below
> ERROR ExceptionMappingInterceptor org.apache.struts2.json.JSONException:
> org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException:
> org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException:
> java.lang.reflect.InvocationTargetException
> org.apache.struts2.json.JSONException:
> org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException:
> org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException:
> org.apache.struts2.json.JSONException:
> java.lang.reflect.InvocationTargetException
> at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:269)
> at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:197)
> at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:182)
> at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:143)
> at org.
> Any idea what's wrong here ? I am using Java 1.8. I think below code in
> JSONWriter is not working as expected
> protected Method findBaseAccessor(Class clazz, Method accessor) {
> Method baseAccessor = null;
> if (clazz.getName().contains("$$EnhancerByCGLIB$$")) {
> try {
> baseAccessor =
> Thread.currentThread().getContextClassLoader().loadClass(
> clazz.getName().substring(0,
> clazz.getName().indexOf("$$"))).getMethod(
> accessor.getName(), accessor.getParameterTypes());
> } catch (Exception ex) {
> LOG.debug(ex.getMessage(), ex);
> }
> } else if (clazz.getName().contains("$$_javassist")) {
> try {
> baseAccessor = Class.forName(
> clazz.getName().substring(0,
> clazz.getName().indexOf("_$$")))
> .getMethod(accessor.getName(),
> accessor.getParameterTypes());
> } catch (Exception ex) {
> LOG.debug(ex.getMessage(), ex);
> }
>
> //in hibernate4.3.7,because javassist3.18.1's class name generate
> rule is '_$$_jvst'+...
> } else if(clazz.getName().contains("$$_jvst")){
> try {
> baseAccessor = Class.forName(
> clazz.getName().substring(0,
> clazz.getName().indexOf("_$$")))
> .getMethod(accessor.getName(),
> accessor.getParameterTypes());
> } catch (Exception ex) {
> LOG.debug(ex.getMessage(), ex);
> }
> }
> else {
> return accessor;
> }
> return baseAccessor;
> }
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)