无法 return json 数据返回到 extjs
Cannot return json data back to extjs
我对 Extjs 和 struts2 完全陌生,正在尝试我自己的一个新项目。但是,在从 struts 操作 class 传递 json 数据时,我不断收到此错误:
Ext.JSON.decode(): 您正在尝试解码无效的 JSON 字符串
我在用:
JDK1.8
阿帕奇 Tomcat 8.5.55
Extjs 6.0.2
IDE日蚀
我的 struts.xml 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="json-default">
<action
name = "generateCustomerJson"
class = "com.proj.Action.CustomerDataJsonCreator"
method = "">
<result name = "Success">/index.jsp
</result>
</action>
</package>
</struts>
我的操作class是:
package com.proj.Action;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.proj.DAO.DBConnector;
import com.proj.SqlQueryGenerator;
public class CustomerDataJsonCreator {
private Connection conn = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private HashMap<String,Object> record= null;
private ArrayList<Object> arrayList = new ArrayList<Object>();
private String data = null;
private JsonElement element = null;
private JsonObject object = new JsonObject();
//connection to DB
public CustomerDataJsonCreator() {
conn = DBConnector.conDB();
}
public String execute() {
setPreparedStatement(SqlQueryGenerator.generator("Select"));
preparedStatement = getPreparedStatement();
setResultSet(preparedStatement);
resultSet = getResultSet();
try {
while (resultSet.next()) {
record = new HashMap<>();
record.put("pk_id",resultSet.getInt(1));
record.put("acct_doc_header_id", resultSet.getInt(2));
record.put("company_id", resultSet.getInt(3));
record.put("document_number", resultSet.getInt(4));
record.put("document_number_norm", resultSet.getInt(5));
record.put("business_code", resultSet.getString(6));
record.put("create_year", resultSet.getString(7));
record.put("document_line_number", resultSet.getInt(8));
record.put("doctype", resultSet.getString(9));
record.put("customer_number", resultSet.getInt(10));
record.put("customer_number_norm", resultSet.getInt(11));
record.put("fk_customer_map_id", resultSet.getInt(12));
record.put("customer_name", resultSet.getString(13));
record.put("division", resultSet.getString(14));
record.put("document_create_date", resultSet.getDate(15));
record.put("document_create_date_norm", resultSet.getDate(16));
record.put("posting_date", resultSet.getDate(17));
record.put("posting_date_norm", resultSet.getDate(18));
record.put("posting_id", resultSet.getString(19));
record.put("due_date", resultSet.getDate(20));
record.put("due_date_norm", resultSet.getDate(21));
record.put("order_date", resultSet.getDate(22));
record.put("order_date_norm", resultSet.getDate(23));
record.put("invoice_id", resultSet.getInt(24));
record.put("invoice_id_norm", resultSet.getInt(25));
record.put("baseline_create_date", resultSet.getDate(26));
record.put("invoice_date_norm", resultSet.getDate(27));
record.put("total_open_amount", resultSet.getFloat(28));
record.put("total_open_amount_norm", resultSet.getFloat(29));
record.put("cust_payment_terms", resultSet.getInt(30));
record.put("business_area", resultSet.getString(31));
record.put("ship_date", resultSet.getDate(32));
record.put("ship_to", resultSet.getString(33));
record.put("clearing_date", resultSet.getDate(34));
record.put("clearing_date_norm", resultSet.getDate(35));
record.put("reason_code", resultSet.getString(36));
record.put("isOpen", resultSet.getInt(37));
record.put("discount_due_date_norm", resultSet.getDate(38));
record.put("debit_credit_indicator", resultSet.getString(39));
record.put("payment_method", resultSet.getString(40));
record.put("document_creation_date", resultSet.getDate(41));
record.put("invoice_amount_doc_currency", resultSet.getFloat(42));
record.put("document_id", resultSet.getInt(43));
record.put("actual_open_amount", resultSet.getFloat(44));
record.put("paid_amount", resultSet.getFloat(45));
record.put("dayspast_due", resultSet.getInt(46));
record.put("invoice_age", resultSet.getInt(47));
record.put("disputed_amount", resultSet.getFloat(48));
setArrayList(record);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
getArrayList();
setJsonData(getArrayList());
getJsonData();
System.out.println(getJsonData());
return "Success";
}
public void setJsonData(ArrayList<Object> arrayList) {
Gson gson = new Gson();
this.element = new JsonParser().parse(gson.toJson(arrayList));
object.add("customerDetail", this.element);
this.data = object.toString();
}
public String getJsonData() {
return this.data;
}
public void setPreparedStatement(String value) {
try {
this.preparedStatement = conn.prepareStatement(value);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PreparedStatement getPreparedStatement() {
return this.preparedStatement;
}
public void setResultSet(PreparedStatement preparedStatement){
try {
this.resultSet = preparedStatement.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet getResultSet() {
return this.resultSet;
}
public void setArrayList(HashMap<String,Object> record) {
arrayList.add(record);
}
public ArrayList<Object> getArrayList(){
return this.arrayList;
}
}
我的 Extjs 代码是:
var gridContainer = function() {
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
'pkId',
'acctDocHeaderId',
'companyId',
'documentNumber',
'documentNumberNorm',
'businessCode',
'createYear',
'documentLineNumber',
'docType',
'customerNumber',
'customerNumberNorm',
'fkCustomerMapId',
'customerName',
'division',
'documentCreateDate',
'documentCreateDateNorm',
'postingDate',
'postingDateNorm',
'postingID',
'dueDate',
'dueDateNorm',
'orderDate',
'orderDateNorm',
'invoiceID',
'invoiceIDNorm',
'baseLineCreateDate',
'invoiceDateNorm',
'totalOpenAmount',
'totalOpenAmountNorm',
'custPaymentTerms',
'businessArea',
'shipDate',
'shipTo',
'clearingDate',
'clearingDateNorm',
'reasonCode',
'isOpen',
'discountDueDateNorm',
'debitCreditIndicator',
'paymentMethod',
'documentCreationDate',
'invoiceAmountDocCurrency',
'documentID',
'actualOpenAmount',
'paidAmount',
'daysPastDue',
'invoiceAge',
'disputedAmount',
]
});
//number of items in a page
var itemsPerPage = 5
//user store
var userStore = Ext.create('Ext.data.Store', {
model: 'User',
pageSize: itemsPerPage,
storeId: 'userStore',
proxy: {
type: 'ajax',
url: 'generateCustomerJson',
// url: 'http://localhost:8080/1705188/customerDataJsonCreator?value='.concat(),
reader: {
type: 'json',
totalProperty: 'total',
rootProperty: 'customerDetail',
}
},
autoLoad: true
});
console.log(userStore);
//business store
var businessStore = Ext.create('Ext.data.Store', {
storeId: 'businessStore',
proxy: {
type: 'ajax',
url: 'http://localhost:8080/1705188/companyCodeJsonCreator',
reader: {
type: 'json',
totalProperty: 'total',
rootProperty: 'customerCode'
}
},
autoLoad: true
});
//sending the data to the server
// userStore.load({
//params: {
//start: 0,
//limit: itemsPerPage,
//}
//});
//grid
Ext.create('Ext.grid.Panel', {
renderTo: Ext.get('ext-js-grid'),
store: userStore,
width: '100%',
id: 'employee-list',
title: 'List of Employees',
selType: 'checkboxmodel',
autoLoad: true,
columns: [{
text: 'Company ID',
sortable: true,
dataIndex: 'pk_id',
flx: 3
}, {
text: 'Account Header ID',
dataIndex: 'acct_doc_header_id',
minWidth: 150
}, {
text: 'Document Number',
dataIndex: 'document_number',
minWidth: 150
}, {
text: 'Document Number Normalised',
dataIndex: 'document_number_norm',
minWidth: 250
}, {
text: 'Business Code',
dataIndex: 'business_code',
minWidth: 150
}, {
text: 'Create Year',
dataIndex: 'create_year',
minWidth: 150
}, {
text: 'Document Line number',
dataIndex: 'document_line_number',
minWidth: 300
}, {
text: 'Document Type',
dataIndex: 'doctype',
minWidth: 150
},{
text: 'Customer Number',
dataIndex: 'customer_number',
minWidth: 150
},{
text: 'Customer Number Normalised',
dataIndex: 'customer_number_norm',
minWidth: 350
}, {
text: 'Customer Map ID',
dataIndex: 'fk_customer_map_id',
minWidth: 150
}, {
text: 'Name Of Customer',
dataIndex: 'customer_name',
minWidth: 150
}, {
text: 'Division',
dataIndex: 'division',
minWidth: 150
}, {
text: 'Document Create Date',
dataIndex: 'document_create_date',
minWidth: 300
}, {
text: 'Document Create Date Normalised',
dataIndex: 'document_create_date_norm',
minWidth: 350
}, {
text: 'Posting date',
dataIndex: 'posting_date',
minWidth: 150
}, {
text: 'Posting Date Normalised',
dataIndex: 'posting_date_norm',
minWidth: 300
}, {
text: 'Posting ID',
dataIndex: 'posting_id',
minWidth: 150
}, {
text: 'Due In Date',
dataIndex: 'due_date',
minWidth: 150
}, {
text: 'Due In Date Normalised',
dataIndex: 'due_date_norm',
minWidth: 300
}, {
text: 'Order create Date',
dataIndex: 'order_date',
minWidth: 150
}, {
text: 'Order Create Date Normalised',
dataIndex: 'order_date_norm',
minWidth: 300
}, {
text: 'Baseline Date',
dataIndex: 'baseline_create_date',
minWidth: 150
}, {
text: 'Invoice Date',
dataIndex: 'invoice_date_norm',
minWidth: 150
}, {
text: 'Invoice ID',
dataIndex: 'invoice_id',
minWidth: 150
}, {
text: 'Invoice ID Normalised',
dataIndex: 'invoice_id_norm',
minWidth: 250
}, {
text: 'Total Open Amount',
dataIndex: 'total_open_amount',
minWidth: 150
},{
text: 'Total Open Amount Normalised',
dataIndex: 'total_open_amount_norm',
minWidth: 350
}, {
text: 'Customer Payment Terms',
dataIndex: 'cust_payment_terms',
minWidth: 300
}, {
text: 'Area of Business',
dataIndex: 'bsiness_area',
minWidth: 150
}, {
text: 'Clear Date',
dataIndex: 'clearing_date',
minWidth: 150
},{
text: 'Clear Date Normalised',
dataIndex: 'clearing_date_norm',
minWidth: 350
}, {
text: 'Is Open Invoice',
dataIndex: 'isopen',
minWidth: 150
}, {
text: 'Shipping Date',
dataIndex: 'ship_date',
minWidth: 150
}, {
text: 'Shipping To',
dataIndex: 'ship_to',
minWidth: 150
},{
text: 'Reason Code',
dataIndex: 'reason_code',
minWidth: 150
}, {
text: 'Discount Due Date Normalised',
dataIndex: 'discount_due_date_norm',
minWidth: 300
}, {
text: 'Debit Credit Status',
dataIndex: 'debit_credit_indicator',
minWidth: 150
}, {
text: 'Payment Method',
dataIndex: 'payment_method',
minWidth: 150
}, {
text: 'Payment Amount',
dataIndex: 'paid_amount',
minWidth: 150
}, {
text: 'Days past Due date',
dataIndex: 'dayspast_due',
minWidth: 150
}, {
text: 'Doc Id',
dataIndex: 'document_id',
minWidth: 150
}, {
text: 'Document Create Date',
dataIndex: 'document_create_date',
minWidth: 350
}, {
text: 'Actual Amount Outstanding',
dataIndex: 'actual_open_amount',
minWidth: 350
}, {
text: 'Age of Invoice',
dataIndex: 'invoice_age',
minWidth: 150
}, {
text: 'Invoice Currency',
dataIndex: 'invoice_amount_doc_currency',
minWidth: 150
}, {
text: 'Dispute Amount',
dataIndex: 'disputed_amount',
minWidth: 150
},],
dockedItems: [{
xtype: 'pagingtoolbar',
store: 'userStore',
dock: 'bottom',
displayInfo: true
}],
buttons: [
{
text: 'Add',
handler: function () {
var userWindow = Ext.create('Ext.window.Window', {
title: 'Add Invoice Details',
height: 600,
width: 600,
layout: 'fit',
bodyPadding: '8',
items: {
xtype: 'form',
border: false,
defaultType: 'textfield',
items: [{
fieldLabel: 'Company Code',
name: 'companyCode',
id: 'companyCode',
xtype: 'combobox',
store: businessStore,
displayField: 'business_code',
queryMode: 'local',
filterPickList :true,
width: '100%',
labelWidth: 150,
allowBlank: false
}, , {
fieldLabel: 'Document Number',
name: 'documentNumber',
id: 'documentNumber',
width: '100%',
vtype: 'alphanum',
labelWidth: 150,
allowBlank: false
}, {
fieldLabel: 'Customer Name',
name: 'customerName',
id: 'customerName',
width: '100%',
labelWidth: 150,
vtype: 'alphanum',
allowBlank: false
}, {
xtype: 'numberfield',
fieldLabel: 'Invoice Id',
name: 'invoiceId',
id :'invoiceId',
width: '100%',
labelWidth: 150,
allowBlank: false
}, {
fieldLabel: 'Open Amount',
name: 'openAmount',
id: 'openAmount',
width: '100%',
labelWidth: 150,
disabled: true,
allowBlank: false
}, {
xtype: 'fieldcontainer',
fieldLabel: 'Is Open Invoice',
defaultType: 'radiofield',
id: 'isOpen',
layout: 'hbox',
labelWidth: 150,
allowBlank: false,
items: [{
boxLabel: 'Yes',
name: 'isOpenInvoice',
inputValue: 'y',
id: 'yesRadioButton',
flex: 1
}, {
boxLabel: 'No',
name: 'isOpenInvoice',
inputValue: 'n',
id: 'noRadioButton',
flex: 1
}]
},{
xtype: 'fieldcontainer',
fieldLabel: 'Payment Method',
defaultType: 'checkboxfield',
id: 'paymentMethod',
layout: 'hbox',
allowBlank: false,
labelWidth: 150,
items: [
{
boxLabel : 'Check',
name : 'paymentMethod',
inputValue: '1',
id : 'checkCheckBox',
checked : true,
flex: 1
}, {
boxLabel : 'ACH',
name : 'paymentMethod',
inputValue: '2',
id : 'achCheckBox',
flex: 1
}, {
boxLabel : 'Online',
name : 'paymentMethod',
inputValue: '3',
id : 'onlineCheckBox'
}
]
}, {
fieldLabel: 'Payment Amount',
name: 'paymentAmount',
id: 'paymentAmount',
width: '100%',
labelWidth: 150,
disabled: true
}, {
fieldLabel: 'Division',
name: 'division',
id: 'division',
width: '100%',
labelWidth: 150,
emptyText: 'Enter Division Email Id',
vtype: 'email'
}],
dockedItems: [{
xtype: 'toolbar',
dock: 'bottom',
layout: {
type: 'hbox',
align: 'center',
pack: 'middle'
},
items: [{
xtype: 'button',
text: 'Save',
formBind: true,
disabled: true,
handler: function() {
var form = this.up('form').getForm().getValues();
console.log(form);
Ext.Ajax.request({
url: 'http://localhost:8080/1705188/addInvoice',
method: 'GET',
params: {
company_id: Ext.getCmp('companyCode').getValue(),
document_number: Ext.getCmp('documentNumber').getValue(),
customer_name: Ext.getCmp('customerName').getValue(),
invoice_id: Ext.getCmp('invoiceId').getValue,
total_open_amount: Ext.getCmp('openAmount').getValue(),
isOpen: Ext.getCmp('yesRadioButton').getValue(),
payment_method: Ext.getCmp('checkCheckBox').getValue(),
paid_amount: Ext.getCmp('paymentAmount').getValue(),
division: Ext.getCmp('division').getValue()
},
success: function(){
Ext.Msg.alert('Saved','Your data has been added');
Ext.getCmp('employee-list').getView().refresh();
},
failure: function(){
Ext.Msg.alert('Error','Your data could not be saved');
}
});
userWindow.close();
}
}, {
xtype: 'button',
text: 'Cancel'
}],
}]
}
});
userWindow.show();
}
}]
});
};
Ext.onReady(gridContainer);
我在 Eclipse 中的控制台输出是:
{"customerDetail":[{"total_open_amount_norm":0.0,"pk_id":1,"customer_number":228448,"business_code":"Faes6","posting_id":"","document_id":544073954,"customer_number_norm":228448,"invoice_age":78,"division":"","reason_code":"","acct_doc_header_id":544073954,"dayspast_due":18,"debit_credit_indicator":"","invoice_id":48289611,"document_line_number":-1,"document_create_date":"Apr 15, 2019","document_create_date_norm":"Apr 15, 2019","payment_method":"","clearing_date":"Jul 2, 2019","document_number":48289611,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"Jul 2, 2019","fk_customer_map_id":-1,"business_area":"","document_number_norm":48289611,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":48289611,"create_year":"","actual_open_amount":8166.52,"paid_amount":8166.52,"disputed_amount":0.0,"customer_name":"Fatboy Industries","ship_to":"","invoice_amount_doc_currency":8166.52},{"total_open_amount_norm":0.0,"pk_id":2,"customer_number":228089,"business_code":"cvrp1","posting_id":"","document_id":540062945,"customer_number_norm":228089,"invoice_age":56,"division":"","reason_code":"","acct_doc_header_id":540062945,"dayspast_due":-4,"debit_credit_indicator":"","invoice_id":40134128,"document_line_number":-1,"document_create_date":"Aug 3, 2018","document_create_date_norm":"Aug 3, 2018","payment_method":"","clearing_date":"Sep 28, 2018","document_number":40134128,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"Sep 28, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":40134128,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":40134128,"create_year":"","actual_open_amount":5027.5,"paid_amount":5027.5,"disputed_amount":0.0,"customer_name":"cvMaker corp","ship_to":"","invoice_amount_doc_currency":5027.5},{"total_open_amount_norm":0.0,"pk_id":3,"customer_number":228098,"business_code":"skes8","posting_id":"","document_id":537291313,"customer_number_norm":228098,"invoice_age":53,"division":"","reason_code":"","acct_doc_header_id":537291313,"dayspast_due":-7,"debit_credit_indicator":"","invoice_id":36548810,"document_line_number":-1,"document_create_date":"Mar 13, 2018","document_create_date_norm":"Mar 13, 2018","payment_method":"","clearing_date":"May 5, 2018","document_number":36548810,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"May 5, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":36548810,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":36548810,"create_year":"","actual_open_amount":270.0,"paid_amount":270.0,"disputed_amount":0.0,"customer_name":"skynetpwc softwares","ship_to":"","invoice_amount_doc_currency":270.0},{"total_open_amount_norm":0.0,"pk_id":4,"customer_number":218997,"business_code":"nuus8","posting_id":"","document_id":539694838,"customer_number_norm":218997,"invoice_age":63,"division":"","reason_code":"","acct_doc_header_id":539694838,"dayspast_due":3,"debit_credit_indicator":"","invoice_id":39579444,"document_line_number":-1,"document_create_date":"Jul 11, 2018","document_create_date_norm":"Jul 11, 2018","payment_method":"","clearing_date":"Sep 12, 2018","document_number":39579444,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"Sep 12, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":39579444,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":39579444,"create_year":"","actual_open_amount":158.94,"paid_amount":158.94,"disputed_amount":0.0,"customer_name":"nucleus","ship_to":"","invoice_amount_doc_currency":158.94},{"total_open_amount_norm":0.0,"pk_id":5,"customer_number":226550,"business_code":"syps7","posting_id":"","document_id":537094052,"customer_number_norm":226550,"invoice_age":62,"division":"","reason_code":"","acct_doc_header_id":537094052,"dayspast_due":2,"debit_credit_indicator":"","invoice_id":36266913,"document_line_number":-1,"document_create_date":"Mar 1, 2018","document_create_date_norm":"Mar 1, 2018","payment_method":"","clearing_date":"May 2, 2018","document_number":36266913,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"May 2, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":36266913,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":36266913,"create_year":"","actual_open_amount":5962.81,"paid_amount":5962.81,"disputed_amount":0.0,"customer_name":"system ops","ship_to":"","invoice_amount_doc_currency":5962.81}]}
我在 运行 服务器时在浏览器上遇到的错误是:
Output of the project
如有任何帮助,我们将不胜感激。
编辑:
我实际上找出了错误的根源。我的 struts2 操作 class 没有返回 json 数据,因此 extjs 无法对其进行解码。我是 struts2 的新手,所以我不知道该怎么做。
编辑:
我的错误已解决。感谢您的帮助。
实际上,问题出在我的 struts.xml 文件中。
已编辑 struts.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="json-default">
<action
name = "generateCustomerJson"
class = "com.highradius.Action.CustomerDataJsonCreator">
<result name = "Success" type="json">
<param name = "root">arrayList</param>
</result>
</action>
</package>
</struts>
您忘记了商店代理和 json 中的 rootProperty。我在 fiddle 中稍微更改了您的代码,它适用于虚拟 jsons.
关于代码
最好将代码拆分为模型、商店和不同的视图。文件必须尽可能小。 (可以是class的一定是class)。例如,弹出 window 最好在单独的 class 中定义(就像在 fiddle 示例中所做的那样)。甚至 grid 和 popup window 的工具栏也必须在单独的 classes.. 中定义。
您的网格列具有相同的属性,最好将它们移动到网格的 'defaults' 配置 属性 中(我已将 labelWith: 150 移到那里)..
我找到了解决方案。经过几天的查找,我在 struts.xml 文件中发现了问题。
更正后的文件附在问题本身中。感谢您的帮助!
我对 Extjs 和 struts2 完全陌生,正在尝试我自己的一个新项目。但是,在从 struts 操作 class 传递 json 数据时,我不断收到此错误:
Ext.JSON.decode(): 您正在尝试解码无效的 JSON 字符串
我在用:
JDK1.8
阿帕奇 Tomcat 8.5.55
Extjs 6.0.2
IDE日蚀
我的 struts.xml 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="json-default">
<action
name = "generateCustomerJson"
class = "com.proj.Action.CustomerDataJsonCreator"
method = "">
<result name = "Success">/index.jsp
</result>
</action>
</package>
</struts>
我的操作class是:
package com.proj.Action;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.proj.DAO.DBConnector;
import com.proj.SqlQueryGenerator;
public class CustomerDataJsonCreator {
private Connection conn = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private HashMap<String,Object> record= null;
private ArrayList<Object> arrayList = new ArrayList<Object>();
private String data = null;
private JsonElement element = null;
private JsonObject object = new JsonObject();
//connection to DB
public CustomerDataJsonCreator() {
conn = DBConnector.conDB();
}
public String execute() {
setPreparedStatement(SqlQueryGenerator.generator("Select"));
preparedStatement = getPreparedStatement();
setResultSet(preparedStatement);
resultSet = getResultSet();
try {
while (resultSet.next()) {
record = new HashMap<>();
record.put("pk_id",resultSet.getInt(1));
record.put("acct_doc_header_id", resultSet.getInt(2));
record.put("company_id", resultSet.getInt(3));
record.put("document_number", resultSet.getInt(4));
record.put("document_number_norm", resultSet.getInt(5));
record.put("business_code", resultSet.getString(6));
record.put("create_year", resultSet.getString(7));
record.put("document_line_number", resultSet.getInt(8));
record.put("doctype", resultSet.getString(9));
record.put("customer_number", resultSet.getInt(10));
record.put("customer_number_norm", resultSet.getInt(11));
record.put("fk_customer_map_id", resultSet.getInt(12));
record.put("customer_name", resultSet.getString(13));
record.put("division", resultSet.getString(14));
record.put("document_create_date", resultSet.getDate(15));
record.put("document_create_date_norm", resultSet.getDate(16));
record.put("posting_date", resultSet.getDate(17));
record.put("posting_date_norm", resultSet.getDate(18));
record.put("posting_id", resultSet.getString(19));
record.put("due_date", resultSet.getDate(20));
record.put("due_date_norm", resultSet.getDate(21));
record.put("order_date", resultSet.getDate(22));
record.put("order_date_norm", resultSet.getDate(23));
record.put("invoice_id", resultSet.getInt(24));
record.put("invoice_id_norm", resultSet.getInt(25));
record.put("baseline_create_date", resultSet.getDate(26));
record.put("invoice_date_norm", resultSet.getDate(27));
record.put("total_open_amount", resultSet.getFloat(28));
record.put("total_open_amount_norm", resultSet.getFloat(29));
record.put("cust_payment_terms", resultSet.getInt(30));
record.put("business_area", resultSet.getString(31));
record.put("ship_date", resultSet.getDate(32));
record.put("ship_to", resultSet.getString(33));
record.put("clearing_date", resultSet.getDate(34));
record.put("clearing_date_norm", resultSet.getDate(35));
record.put("reason_code", resultSet.getString(36));
record.put("isOpen", resultSet.getInt(37));
record.put("discount_due_date_norm", resultSet.getDate(38));
record.put("debit_credit_indicator", resultSet.getString(39));
record.put("payment_method", resultSet.getString(40));
record.put("document_creation_date", resultSet.getDate(41));
record.put("invoice_amount_doc_currency", resultSet.getFloat(42));
record.put("document_id", resultSet.getInt(43));
record.put("actual_open_amount", resultSet.getFloat(44));
record.put("paid_amount", resultSet.getFloat(45));
record.put("dayspast_due", resultSet.getInt(46));
record.put("invoice_age", resultSet.getInt(47));
record.put("disputed_amount", resultSet.getFloat(48));
setArrayList(record);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
getArrayList();
setJsonData(getArrayList());
getJsonData();
System.out.println(getJsonData());
return "Success";
}
public void setJsonData(ArrayList<Object> arrayList) {
Gson gson = new Gson();
this.element = new JsonParser().parse(gson.toJson(arrayList));
object.add("customerDetail", this.element);
this.data = object.toString();
}
public String getJsonData() {
return this.data;
}
public void setPreparedStatement(String value) {
try {
this.preparedStatement = conn.prepareStatement(value);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PreparedStatement getPreparedStatement() {
return this.preparedStatement;
}
public void setResultSet(PreparedStatement preparedStatement){
try {
this.resultSet = preparedStatement.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet getResultSet() {
return this.resultSet;
}
public void setArrayList(HashMap<String,Object> record) {
arrayList.add(record);
}
public ArrayList<Object> getArrayList(){
return this.arrayList;
}
}
我的 Extjs 代码是:
var gridContainer = function() {
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
'pkId',
'acctDocHeaderId',
'companyId',
'documentNumber',
'documentNumberNorm',
'businessCode',
'createYear',
'documentLineNumber',
'docType',
'customerNumber',
'customerNumberNorm',
'fkCustomerMapId',
'customerName',
'division',
'documentCreateDate',
'documentCreateDateNorm',
'postingDate',
'postingDateNorm',
'postingID',
'dueDate',
'dueDateNorm',
'orderDate',
'orderDateNorm',
'invoiceID',
'invoiceIDNorm',
'baseLineCreateDate',
'invoiceDateNorm',
'totalOpenAmount',
'totalOpenAmountNorm',
'custPaymentTerms',
'businessArea',
'shipDate',
'shipTo',
'clearingDate',
'clearingDateNorm',
'reasonCode',
'isOpen',
'discountDueDateNorm',
'debitCreditIndicator',
'paymentMethod',
'documentCreationDate',
'invoiceAmountDocCurrency',
'documentID',
'actualOpenAmount',
'paidAmount',
'daysPastDue',
'invoiceAge',
'disputedAmount',
]
});
//number of items in a page
var itemsPerPage = 5
//user store
var userStore = Ext.create('Ext.data.Store', {
model: 'User',
pageSize: itemsPerPage,
storeId: 'userStore',
proxy: {
type: 'ajax',
url: 'generateCustomerJson',
// url: 'http://localhost:8080/1705188/customerDataJsonCreator?value='.concat(),
reader: {
type: 'json',
totalProperty: 'total',
rootProperty: 'customerDetail',
}
},
autoLoad: true
});
console.log(userStore);
//business store
var businessStore = Ext.create('Ext.data.Store', {
storeId: 'businessStore',
proxy: {
type: 'ajax',
url: 'http://localhost:8080/1705188/companyCodeJsonCreator',
reader: {
type: 'json',
totalProperty: 'total',
rootProperty: 'customerCode'
}
},
autoLoad: true
});
//sending the data to the server
// userStore.load({
//params: {
//start: 0,
//limit: itemsPerPage,
//}
//});
//grid
Ext.create('Ext.grid.Panel', {
renderTo: Ext.get('ext-js-grid'),
store: userStore,
width: '100%',
id: 'employee-list',
title: 'List of Employees',
selType: 'checkboxmodel',
autoLoad: true,
columns: [{
text: 'Company ID',
sortable: true,
dataIndex: 'pk_id',
flx: 3
}, {
text: 'Account Header ID',
dataIndex: 'acct_doc_header_id',
minWidth: 150
}, {
text: 'Document Number',
dataIndex: 'document_number',
minWidth: 150
}, {
text: 'Document Number Normalised',
dataIndex: 'document_number_norm',
minWidth: 250
}, {
text: 'Business Code',
dataIndex: 'business_code',
minWidth: 150
}, {
text: 'Create Year',
dataIndex: 'create_year',
minWidth: 150
}, {
text: 'Document Line number',
dataIndex: 'document_line_number',
minWidth: 300
}, {
text: 'Document Type',
dataIndex: 'doctype',
minWidth: 150
},{
text: 'Customer Number',
dataIndex: 'customer_number',
minWidth: 150
},{
text: 'Customer Number Normalised',
dataIndex: 'customer_number_norm',
minWidth: 350
}, {
text: 'Customer Map ID',
dataIndex: 'fk_customer_map_id',
minWidth: 150
}, {
text: 'Name Of Customer',
dataIndex: 'customer_name',
minWidth: 150
}, {
text: 'Division',
dataIndex: 'division',
minWidth: 150
}, {
text: 'Document Create Date',
dataIndex: 'document_create_date',
minWidth: 300
}, {
text: 'Document Create Date Normalised',
dataIndex: 'document_create_date_norm',
minWidth: 350
}, {
text: 'Posting date',
dataIndex: 'posting_date',
minWidth: 150
}, {
text: 'Posting Date Normalised',
dataIndex: 'posting_date_norm',
minWidth: 300
}, {
text: 'Posting ID',
dataIndex: 'posting_id',
minWidth: 150
}, {
text: 'Due In Date',
dataIndex: 'due_date',
minWidth: 150
}, {
text: 'Due In Date Normalised',
dataIndex: 'due_date_norm',
minWidth: 300
}, {
text: 'Order create Date',
dataIndex: 'order_date',
minWidth: 150
}, {
text: 'Order Create Date Normalised',
dataIndex: 'order_date_norm',
minWidth: 300
}, {
text: 'Baseline Date',
dataIndex: 'baseline_create_date',
minWidth: 150
}, {
text: 'Invoice Date',
dataIndex: 'invoice_date_norm',
minWidth: 150
}, {
text: 'Invoice ID',
dataIndex: 'invoice_id',
minWidth: 150
}, {
text: 'Invoice ID Normalised',
dataIndex: 'invoice_id_norm',
minWidth: 250
}, {
text: 'Total Open Amount',
dataIndex: 'total_open_amount',
minWidth: 150
},{
text: 'Total Open Amount Normalised',
dataIndex: 'total_open_amount_norm',
minWidth: 350
}, {
text: 'Customer Payment Terms',
dataIndex: 'cust_payment_terms',
minWidth: 300
}, {
text: 'Area of Business',
dataIndex: 'bsiness_area',
minWidth: 150
}, {
text: 'Clear Date',
dataIndex: 'clearing_date',
minWidth: 150
},{
text: 'Clear Date Normalised',
dataIndex: 'clearing_date_norm',
minWidth: 350
}, {
text: 'Is Open Invoice',
dataIndex: 'isopen',
minWidth: 150
}, {
text: 'Shipping Date',
dataIndex: 'ship_date',
minWidth: 150
}, {
text: 'Shipping To',
dataIndex: 'ship_to',
minWidth: 150
},{
text: 'Reason Code',
dataIndex: 'reason_code',
minWidth: 150
}, {
text: 'Discount Due Date Normalised',
dataIndex: 'discount_due_date_norm',
minWidth: 300
}, {
text: 'Debit Credit Status',
dataIndex: 'debit_credit_indicator',
minWidth: 150
}, {
text: 'Payment Method',
dataIndex: 'payment_method',
minWidth: 150
}, {
text: 'Payment Amount',
dataIndex: 'paid_amount',
minWidth: 150
}, {
text: 'Days past Due date',
dataIndex: 'dayspast_due',
minWidth: 150
}, {
text: 'Doc Id',
dataIndex: 'document_id',
minWidth: 150
}, {
text: 'Document Create Date',
dataIndex: 'document_create_date',
minWidth: 350
}, {
text: 'Actual Amount Outstanding',
dataIndex: 'actual_open_amount',
minWidth: 350
}, {
text: 'Age of Invoice',
dataIndex: 'invoice_age',
minWidth: 150
}, {
text: 'Invoice Currency',
dataIndex: 'invoice_amount_doc_currency',
minWidth: 150
}, {
text: 'Dispute Amount',
dataIndex: 'disputed_amount',
minWidth: 150
},],
dockedItems: [{
xtype: 'pagingtoolbar',
store: 'userStore',
dock: 'bottom',
displayInfo: true
}],
buttons: [
{
text: 'Add',
handler: function () {
var userWindow = Ext.create('Ext.window.Window', {
title: 'Add Invoice Details',
height: 600,
width: 600,
layout: 'fit',
bodyPadding: '8',
items: {
xtype: 'form',
border: false,
defaultType: 'textfield',
items: [{
fieldLabel: 'Company Code',
name: 'companyCode',
id: 'companyCode',
xtype: 'combobox',
store: businessStore,
displayField: 'business_code',
queryMode: 'local',
filterPickList :true,
width: '100%',
labelWidth: 150,
allowBlank: false
}, , {
fieldLabel: 'Document Number',
name: 'documentNumber',
id: 'documentNumber',
width: '100%',
vtype: 'alphanum',
labelWidth: 150,
allowBlank: false
}, {
fieldLabel: 'Customer Name',
name: 'customerName',
id: 'customerName',
width: '100%',
labelWidth: 150,
vtype: 'alphanum',
allowBlank: false
}, {
xtype: 'numberfield',
fieldLabel: 'Invoice Id',
name: 'invoiceId',
id :'invoiceId',
width: '100%',
labelWidth: 150,
allowBlank: false
}, {
fieldLabel: 'Open Amount',
name: 'openAmount',
id: 'openAmount',
width: '100%',
labelWidth: 150,
disabled: true,
allowBlank: false
}, {
xtype: 'fieldcontainer',
fieldLabel: 'Is Open Invoice',
defaultType: 'radiofield',
id: 'isOpen',
layout: 'hbox',
labelWidth: 150,
allowBlank: false,
items: [{
boxLabel: 'Yes',
name: 'isOpenInvoice',
inputValue: 'y',
id: 'yesRadioButton',
flex: 1
}, {
boxLabel: 'No',
name: 'isOpenInvoice',
inputValue: 'n',
id: 'noRadioButton',
flex: 1
}]
},{
xtype: 'fieldcontainer',
fieldLabel: 'Payment Method',
defaultType: 'checkboxfield',
id: 'paymentMethod',
layout: 'hbox',
allowBlank: false,
labelWidth: 150,
items: [
{
boxLabel : 'Check',
name : 'paymentMethod',
inputValue: '1',
id : 'checkCheckBox',
checked : true,
flex: 1
}, {
boxLabel : 'ACH',
name : 'paymentMethod',
inputValue: '2',
id : 'achCheckBox',
flex: 1
}, {
boxLabel : 'Online',
name : 'paymentMethod',
inputValue: '3',
id : 'onlineCheckBox'
}
]
}, {
fieldLabel: 'Payment Amount',
name: 'paymentAmount',
id: 'paymentAmount',
width: '100%',
labelWidth: 150,
disabled: true
}, {
fieldLabel: 'Division',
name: 'division',
id: 'division',
width: '100%',
labelWidth: 150,
emptyText: 'Enter Division Email Id',
vtype: 'email'
}],
dockedItems: [{
xtype: 'toolbar',
dock: 'bottom',
layout: {
type: 'hbox',
align: 'center',
pack: 'middle'
},
items: [{
xtype: 'button',
text: 'Save',
formBind: true,
disabled: true,
handler: function() {
var form = this.up('form').getForm().getValues();
console.log(form);
Ext.Ajax.request({
url: 'http://localhost:8080/1705188/addInvoice',
method: 'GET',
params: {
company_id: Ext.getCmp('companyCode').getValue(),
document_number: Ext.getCmp('documentNumber').getValue(),
customer_name: Ext.getCmp('customerName').getValue(),
invoice_id: Ext.getCmp('invoiceId').getValue,
total_open_amount: Ext.getCmp('openAmount').getValue(),
isOpen: Ext.getCmp('yesRadioButton').getValue(),
payment_method: Ext.getCmp('checkCheckBox').getValue(),
paid_amount: Ext.getCmp('paymentAmount').getValue(),
division: Ext.getCmp('division').getValue()
},
success: function(){
Ext.Msg.alert('Saved','Your data has been added');
Ext.getCmp('employee-list').getView().refresh();
},
failure: function(){
Ext.Msg.alert('Error','Your data could not be saved');
}
});
userWindow.close();
}
}, {
xtype: 'button',
text: 'Cancel'
}],
}]
}
});
userWindow.show();
}
}]
});
};
Ext.onReady(gridContainer);
我在 Eclipse 中的控制台输出是:
{"customerDetail":[{"total_open_amount_norm":0.0,"pk_id":1,"customer_number":228448,"business_code":"Faes6","posting_id":"","document_id":544073954,"customer_number_norm":228448,"invoice_age":78,"division":"","reason_code":"","acct_doc_header_id":544073954,"dayspast_due":18,"debit_credit_indicator":"","invoice_id":48289611,"document_line_number":-1,"document_create_date":"Apr 15, 2019","document_create_date_norm":"Apr 15, 2019","payment_method":"","clearing_date":"Jul 2, 2019","document_number":48289611,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"Jul 2, 2019","fk_customer_map_id":-1,"business_area":"","document_number_norm":48289611,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":48289611,"create_year":"","actual_open_amount":8166.52,"paid_amount":8166.52,"disputed_amount":0.0,"customer_name":"Fatboy Industries","ship_to":"","invoice_amount_doc_currency":8166.52},{"total_open_amount_norm":0.0,"pk_id":2,"customer_number":228089,"business_code":"cvrp1","posting_id":"","document_id":540062945,"customer_number_norm":228089,"invoice_age":56,"division":"","reason_code":"","acct_doc_header_id":540062945,"dayspast_due":-4,"debit_credit_indicator":"","invoice_id":40134128,"document_line_number":-1,"document_create_date":"Aug 3, 2018","document_create_date_norm":"Aug 3, 2018","payment_method":"","clearing_date":"Sep 28, 2018","document_number":40134128,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"Sep 28, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":40134128,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":40134128,"create_year":"","actual_open_amount":5027.5,"paid_amount":5027.5,"disputed_amount":0.0,"customer_name":"cvMaker corp","ship_to":"","invoice_amount_doc_currency":5027.5},{"total_open_amount_norm":0.0,"pk_id":3,"customer_number":228098,"business_code":"skes8","posting_id":"","document_id":537291313,"customer_number_norm":228098,"invoice_age":53,"division":"","reason_code":"","acct_doc_header_id":537291313,"dayspast_due":-7,"debit_credit_indicator":"","invoice_id":36548810,"document_line_number":-1,"document_create_date":"Mar 13, 2018","document_create_date_norm":"Mar 13, 2018","payment_method":"","clearing_date":"May 5, 2018","document_number":36548810,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"May 5, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":36548810,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":36548810,"create_year":"","actual_open_amount":270.0,"paid_amount":270.0,"disputed_amount":0.0,"customer_name":"skynetpwc softwares","ship_to":"","invoice_amount_doc_currency":270.0},{"total_open_amount_norm":0.0,"pk_id":4,"customer_number":218997,"business_code":"nuus8","posting_id":"","document_id":539694838,"customer_number_norm":218997,"invoice_age":63,"division":"","reason_code":"","acct_doc_header_id":539694838,"dayspast_due":3,"debit_credit_indicator":"","invoice_id":39579444,"document_line_number":-1,"document_create_date":"Jul 11, 2018","document_create_date_norm":"Jul 11, 2018","payment_method":"","clearing_date":"Sep 12, 2018","document_number":39579444,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"Sep 12, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":39579444,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":39579444,"create_year":"","actual_open_amount":158.94,"paid_amount":158.94,"disputed_amount":0.0,"customer_name":"nucleus","ship_to":"","invoice_amount_doc_currency":158.94},{"total_open_amount_norm":0.0,"pk_id":5,"customer_number":226550,"business_code":"syps7","posting_id":"","document_id":537094052,"customer_number_norm":226550,"invoice_age":62,"division":"","reason_code":"","acct_doc_header_id":537094052,"dayspast_due":2,"debit_credit_indicator":"","invoice_id":36266913,"document_line_number":-1,"document_create_date":"Mar 1, 2018","document_create_date_norm":"Mar 1, 2018","payment_method":"","clearing_date":"May 2, 2018","document_number":36266913,"company_id":60,"total_open_amount":0.0,"clearing_date_norm":"May 2, 2018","fk_customer_map_id":-1,"business_area":"","document_number_norm":36266913,"doctype":"RI","cust_payment_terms":60,"isOpen":0,"invoice_id_norm":36266913,"create_year":"","actual_open_amount":5962.81,"paid_amount":5962.81,"disputed_amount":0.0,"customer_name":"system ops","ship_to":"","invoice_amount_doc_currency":5962.81}]}
我在 运行 服务器时在浏览器上遇到的错误是: Output of the project
如有任何帮助,我们将不胜感激。
编辑:
我实际上找出了错误的根源。我的 struts2 操作 class 没有返回 json 数据,因此 extjs 无法对其进行解码。我是 struts2 的新手,所以我不知道该怎么做。
编辑:
我的错误已解决。感谢您的帮助。
实际上,问题出在我的 struts.xml 文件中。
已编辑 struts.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="json-default">
<action
name = "generateCustomerJson"
class = "com.highradius.Action.CustomerDataJsonCreator">
<result name = "Success" type="json">
<param name = "root">arrayList</param>
</result>
</action>
</package>
</struts>
关于代码 最好将代码拆分为模型、商店和不同的视图。文件必须尽可能小。 (可以是class的一定是class)。例如,弹出 window 最好在单独的 class 中定义(就像在 fiddle 示例中所做的那样)。甚至 grid 和 popup window 的工具栏也必须在单独的 classes.. 中定义。 您的网格列具有相同的属性,最好将它们移动到网格的 'defaults' 配置 属性 中(我已将 labelWith: 150 移到那里)..
我找到了解决方案。经过几天的查找,我在 struts.xml 文件中发现了问题。 更正后的文件附在问题本身中。感谢您的帮助!