我试图从js获取JSON数据并在操作类中接收null [英] Im trying to get JSON data from js and receiving null in action class
问题描述
我从客户端获取JSON.我想通过ajax发送它.但是,我的动作类内部为空.
I'm getting JSON from client side. I want to send it through ajax. However, i'm getting null inside my action class.
我的JSON字符串
{"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
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
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";
}
}
控制台日志似乎显示了JSON的正确格式.有人请帮我谢谢!
The console log seems to display a correct format for JSON. Someone pls help me out thanks!
推荐答案
如果您需要产生 JSON(操作-> JSP),则 json
结果为足够的.
If you need to produce JSON (Action -> JSP), the json
result is enough.
如果您需要使用 JSON(JSP->操作),则需要将 Json拦截器添加到堆栈中.
If you need to consume JSON (JSP -> Action), you need to add the Json Interceptor to your stack.
来自文档:
接受JSON
如果您的操作位于程序包中,则可以接受传入的JSON 使用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")
还请注意,json-default
包在内部扩展了struts-default
,因此无需扩展它们两个.
Also note that the json-default
package internally extends the struts-default
, hence there is no need to extend both of them.
应该是:
<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>
这篇关于我试图从js获取JSON数据并在操作类中接收null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!