Im trying to get JSON data from js and receiving null in action class

淺唱寂寞╮ 提交于 2019-12-22 17:29:20

问题


I'm getting JSON from client side. I want to send it through ajax. However, i'm getting null inside my action class.

my JSON string

{"data":[{"id":"","col":1,"row":1,"size_x":1,"size_y":1},
         {"id":"","col":1,"row":2,"size_x":1,"size_y":1},
         {"id":"","col":1,"row":3,"size_x":1,"size_y":1},
         {"id":"","col":1,"row":4,"size_x":1,"size_y":1},
         {"id":"","col":1,"row":6,"size_x":1,"size_y":1},
         {"id":"","col":1,"row":5,"size_x":1,"size_y":1}
        ]
}

AJAX

var seats = {"data" : positions};

    var seatPosition = JSON.stringify(seats);

    console.log(seatPosition);  

    $.ajax({
        url: 'retrieve-seat',
        data: seatPosition,
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        async: true,
        success: function (data) {
        alert("success");

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="," />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.custom.i18n.resources" value="global" />

<package name="default" namespace="/"
    extends="json-default,struts-default">

    <action name="retrieve-seat" class="com.pointwest.apex.actions.ReadPositionAction">
        <result name="SUCCESS" type="json"/>
    </action>

</package>

</struts>

Action.class

public class ReadPositionAction extends ActionSupport {

private List<Seat> data;


public List<Seat> getData() {
     System.out.println("Getter Call");
    return data;
}

public void setData(List<Seat> data) {
     System.out.println("Setter Call");
    this.data = data;
}

@Override
public String execute() {
    System.out.println("action");
    try {
        System.out.println(data);


        System.out.println("Execute Method");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "SUCCESS";
}

}

The console log seems to display a correct format for JSON. Someone pls help me out thanks!


回答1:


If you need to produce JSON (Action -> JSP), the json result is enough.

If you need to consume JSON (JSP -> Action), you need to add the Json Interceptor to your stack.

From the documentation:

Accepting JSON

Your actions can accept incoming JSON if they are in package which uses json interceptor or by adding reference to it as follow:

@InterceptorRef(value="json")

Also note that the json-default package internally extends the struts-default, hence there is no need to extend both of them.

It should be:

<package name="default" namespace="/" extends="json-default">

    <interceptor-stack name="jsonFromJspStack">
        <interceptor-ref name="defaultStack"/>
        <interceptor-ref name="json" />
    </interceptor-stack>


    <action name="retrieve-seat" class="com.pointwest.apex.actions.ReadPositionAction">
        <interceptor-ref name="jsonFromJspStack"/>
        <result name="SUCCESS" type="json"/>
    </action>

</package>


来源:https://stackoverflow.com/questions/33363140/im-trying-to-get-json-data-from-js-and-receiving-null-in-action-class

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