我使用struts2-json插件生成JSON数据和Ajax,用来自该JSON(according to the source)的数据填充表(handsontable)。
现在,我需要使用JSON的Ajax从表中检索数据到Struts2 Action。 首先,我已经使用JSON从Struts2 Action传递给Handsontable的数据实现了填充表,这非常简单且有效。但为什么保存不起作用,你可以在下面的附加代码中看到?
正如我在firebug中看到的那样发送了POST,并且在调试中,我的JSONSaveAction操作中检索到了请求,但是字段数据没有填充JSON数据,为什么?不应该通过struts2-json插件自动将数据绑定到java对象?我究竟做错了什么?
在handontable部分,函数handsontable.getData()
负责从表中获取整个数据。所以我这样使用它但没有成功:
$.ajax({
url: "../json/saveJSONDataAction.action",
data: {data: handsontable.getData()}, //returns all cells' data
dataType: 'json',
type: 'POST',
success: function (res) {
if (res.result === 'ok') {
$console.text('Data saved');
}
else {
$console.text('Save error');
}
}
});
函数handsontable.getData()
实际上检索了我检查的所有数据,但是在我的JSONSaveAction操作中,数据没有绑定到java对象List<Report> data
。你知道为什么吗?
以下是POST请求后我的表和firebug信息的屏幕截图:
@ParentPackage("json-default")
@Action(value="getJSONDataAction")
@Result(name="success", type="json")
public class JSONDataAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<Report> data = new ArrayList<Report>();
public JSONDataAction(){
data.add(new Report(1, "Chris", true, "2008-01-01", "orange"));
data.add(new Report(2, "Kate", false, "2013-03-03", "blue"));
data.add(new Report(3, "Blade", true, "2013-05-03", "black"));
data.add(new Report(4, "Zack", false, "2013-01-01", "yellow"));
}
public String execute() {
return SUCCESS;
}
public List<Report> getData() {
return data;
}
public void setData(List<Report> data) {
this.data = data;
}
}
发送给自动生成的表填充的JSON:
{"data":[
{"active":true,"color":"orange","date":"2008-01-01","id":1,"name":"Chris"},
{"active":false,"color":"blue","date":"2013-03-03","id":2,"name":"Kate"},
{"active":true,"color":"black","date":"2013-05-03","id":3,"name":"Blade"},
{"active":false,"color":"yellow","date":"2013-01-01","id":4,"name":"Zack"}]
}
此处字段List<Report> data
始终为null,未填充JSON中的数据:(
@ParentPackage("json-default")
@Action(value="saveJSONDataAction")
@Result(name="success", type="json")
public class JSONSaveAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<Report> data;
public JSONSaveAction(){
}
public String execute() {
try {
JSONObject jsonData = (JSONObject) JSONSerializer.toJSON(data);
String name = jsonData.getString("name");
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
public List<Report> getData() {
return data;
}
public void setData(List<Report> data) {
this.data = data;
}
}
报告类:
public class Report {
private int id;
private String name;
private boolean active;
private String date;
private String color;
//getters and setters
}
<div id="spreadsheet">
<p>
<button type="button" name="load">Load</button>
<button type="button" name="save">Save</button>
</p>
</div>
<div id="console" class="console"></div>
<script>
var $container = $("#spreadsheet");
var $console = $("#console");
var $parent = $container.parent();
$container.handsontable({
startRows: 4,
startCols: 20,
rowHeaders: true,
colHeaders: true,
contextMenu: true,
columns: [
{data: "id", type: 'text'},
{data: "name", type: 'text'},
{data: "active", type: 'checkbox'},
{data: "date", type: 'date'},
{data: "color",
type: 'autocomplete',
source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white"]
}
]
});
var handsontable = $container.data('handsontable');
$parent.find('button[name=load]').click(function () {
$.ajax({
url: "../json/getJSONDataAction.action",
dataType: 'json',
type: 'GET',
success: function (res) {
handsontable.loadData(res.data);
$console.text('Data loaded');
}
});
});
$parent.find('button[name=save]').click(function () {
$.ajax({
url: "../json/saveJSONDataAction.action",
data: {data: handsontable.getData()}, //returns all cells' data
dataType: 'json',
type: 'POST',
success: function (res) {
if (res.result === 'ok') {
$console.text('Data saved');
}
else {
$console.text('Save error');
}
},
error: function () {
$console.text('Save error.');
}
});
});
</script>
请帮我如何正确地从表格检索数据到java对象,因为它确实阻止了我。我不知道我做错了什么......
非常感谢任何意见!
答案 0 :(得分:3)
我已经修好了。
1:在struts.xml中添加:
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
2:在Ajax请求中添加:
contentType: 'application/json'
3:更改JSON格式,该格式由handontable自动格式化。在JSON中有一些字符如:%5B%5D%7B%7D%22而不是:[] {}“
我已经用自己的fixedEncodeURI()函数替换了它们:
var data = '{"data":' + fixedEncodeURI(JSON.stringify(handsontable.getData())) + '}';
$.ajax({
url: "../json/saveJSONDataAction.action",
data: data, //returns all cells' data
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: function (res) {
}
});
function fixedEncodeURI (str) {
return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%7B/g, '{').replace(/%7D/g, '}').replace(/%22/g, '"');
}
答案 1 :(得分:1)
我没有仔细查看过您的代码,但初看起来,当您在data
中使用new ArrayList<Report>()
创建新代码时,您似乎正在删除Action
集合。请单独留下声明private List<Report> data;
。