我正在尝试使用 Ext direct 的 Remoting API 来调用远程方法。但是在点击以下 URL 时出现错误:“http://10.112.202.164:8080/TestProject/app.html”
错误:通过 XHR 同步加载失败:'\TestProject\direct\undefined.js' 请验证文件是否存在。
XHR 状态码:404 这个 GET 请求是在 POST 请求之后发送的,POST 请求很好地获取了所需的响应,但是之后这个 GET 请求被触发了,不知道为什么?
我的代码如下:
应用程序.html:
<!DOCTYPE html>
<!-- Auto Generated with Sencha Architect -->
<!-- Modifications to this file will be overwritten. -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MyExtDirct</title>
<link rel="stylesheet" type="text/css" href="lib/resources/css/sencha-touch.css"/>
<script type="text/javascript" src="lib/sencha-touch-all-debug.js"></script>
<script type="text/javascript">
Ext.ns("Ext.app.REMOTING_API");
Ext.app.REMOTING_API = {"descriptor":"Ext.app.REMOTING_API","url":"http://10.112.202.164:8080/TestProject/AjaxHandler","type":"remoting","actions":{"TestAction":[{"name":"getCountry","len":0,"formHandler":false}]}};
Ext.Direct.addProvider(Ext.app.REMOTING_API);
</script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript">
if (!Ext.browser.is.WebKit) {
alert("The current browser is unsupported.\n\nSupported browsers:\n" +
"Google Chrome\n" +
"Apple Safari\n" +
"Mobile Safari (iOS)\n" +
"Android Browser\n" +
"BlackBerry Browser"
);
}
</script>
</head>
<body></body>
</html>
应用程序.js:
Ext.require([
'Ext.direct.*'
]);
Ext.application({
stores: [
'MyDirectStore'
],
views: [
'MyFormPanel'
],
name: 'MyApp',
launch: function() {
Ext.create('MyApp.view.MyFormPanel', {fullscreen: true});
}
});
MyDirectStore.js:
Ext.define('MyApp.store.MyDirectStore', {
extend: 'Ext.data.Store',
config: {
autoLoad: true,
storeId: 'MyDirectStore',
proxy: {
type: 'direct',
directFn: TestAction.getCountry,
reader: {
type: 'json',
record: 'countryName'
}
},
fields: [
{
name: 'countryName'
}
],
listeners: [
{
fn: 'onStoreLoad',
event: 'load'
}
]
},
onStoreLoad: function(store, records, successful, operation, eOpts) {
Ext.Msg.alert( "Information", "Loaded " + records.length + " records");
}
});
MyFormPanel.js:
Ext.define('MyApp.view.MyFormPanel', {
extend: 'Ext.form.Panel',
config: {
items: [
{
xtype: 'textfield',
itemId: 'mytextfield',
label: 'Country',
store: 'MyDirectStore'
}
],
listeners: [
{
fn: 'onMytextfieldFocus',
event: 'focus',
delegate: '#mytextfield'
}
]
},
onMytextfieldFocus: function(textfield, e, options) {
var store = Ext.getStore('MyDirectStore');
store.load();
TestAction.getCountry(function(result, event) {
var transaction = event.getTransaction(),
content;
if (event.getStatus()) {
content = Ext.String.format('<b>Successful call to {0}.{1} with response:</b><pre>{2}</pre>',
transaction.getAction(), transaction.getMethod(), Ext.encode(result));
} else {
content = Ext.String.format('<b>Call to {0}.{1} failed with message:</b><pre>{2}</pre>',
transaction.getAction(), transaction.getMethod(), event.getMessage());
}
//updateMain(content);
//field.reset();
});
}
});
测试动作.java:
public class TestAction {
public String getCountry()throws JSONException{
String country = "India";
JSONObject object = new JSONObject();
object.put("countryName", country);
object.put("success", true);
String result = object.toString();
return result;
}
}
AjaxHandler.java:
public class AjaxHandler extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AjaxHandler() {
super();
// TODO Auto-generated constructor stub
// Object o= SecurityContextHolder.getContext().getAuthentication();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
handleAjaxRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
handleAjaxRequest(request, response);
}
private void handleAjaxRequest(HttpServletRequest request, HttpServletResponse response)throws IOException{
String className = "";
String methodName = "";
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request. getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) { /*report an error*/ }
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(jb.toString());
} catch (Exception e) {
// crash and burn
throw new IOException("Error parsing JSON request string");
}
try {
className = jsonObject.getString("action");
methodName = jsonObject.getString("method");
if(className!=null && methodName!=null){
Object obj =Class.forName("com.hcl.ml.bean" +"."+ className).newInstance();
//Class[] types = new Class[] {HttpServletRequest.class};
Method method = obj.getClass().getMethod(methodName);
Object data = method.invoke(obj);
reply(response, data.toString());
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void reply(HttpServletResponse response,String data) throws IOException{
response.setContentType("application/json;charset=UTF-8");
//response.setContentType("text/xml");
//ServletOutputStream out = response.getOutputStream();
//out.println(data);
PrintWriter out = response.getWriter();
out.print(data);
}
}