在使用JSP Ajax响应 [英] Use an Ajax response in JSP
问题描述
我有一个 JSP 的网页,其中有只是一个普通的HTML表格与五行,五列。
I have a JSP page which has nothing but a normal HTML table with five rows and five columns.
现在我想提出一个Ajax调用,并获得回应。现在,一旦我有反应后,我需要的数据,以填写表格的相应单元格。
Now I am making an Ajax call and get a response back. Now once I have the response back, I need the data to be filled in appropriate cells of the table.
所以我的问题是;
- 我应该使用 JSON 建设有何反应?
- 如何处理数据回到了JSP的水平。也就是说,一旦我有一个从服务器的响应?
- Should I use JSON for building the response?
- How do I handle the data back at the JSP level. That is, once I have the response from the server?
正如更多的信息,我使用 DWR 这只不过是调用一个Java方法(它建立响应)从里面的JavaScript code。
Just as additional information, I am using DWR which is nothing but calling a Java method (which builds the response) from inside JavaScript code.
推荐答案
让我们考虑这个Java类。
Let's consider this Java class.
class Employee
{
int id;
String eName;
// Setters and getters
}
在JavaScript中,JSON对象:
In JavaScript, the JSON object:
var employee = {
id : null,
name : null
};
这是从JavaScript函数调用Java方法:
This is the call to a Java method from a JavaScript function:
EmployeeUtil.getRow(employee,dwrData);
在EmployeeUtil类中的getRow()方法的返回类型将是公司雇员:
In getRow() of the EmployeeUtil class, the return type of method will be Employee:
Employee getRow();
员工的
因此,使用setter方法设置数据。 dwrData是回调函数。
So using the setters of Employee set the data. dwrData is the callback function.
function dwrData(data) {
employee=data;
}
返回的数据,这是一个员工的豆,将在回调函数。
The data returned, which is an Employee bean, will be in the callback function.
只是初始化这个在JavaScript的JSON对象。
Just initialize this in the JavaScript JSON object.
使用JSON对象据此来填充表。
Use a JSON object accordingly to populate the table.
编辑:
您可以使用列表中的getRow()
而不是员工中的getRow()
,返回行作为一个列表列表,而不是一个Bean。
You can use List getRow()
instead of Employee getRow()
, returning a list of rows as a List instead of a Bean.
现在响应包含列表数据。
Now the response contains list as data.
请参照的 填充使用DWR行 。
Refer to Populate rows using DWR.
检查这些例子来填充表中的数据:
Check these examples to populate data in table:
- DWR + Dojo Demo
- Dynamically Editing a Table
我应该使用JSON构建反应?
Should I use JSON for building the response?
-
没有必要通过JSON响应。相反,返回上述一类的豆。
No need to pass JSON in response. Instead return a Bean of a class as mentioned above.
上述名单可以传递作为响应,也。
A list can be passed as a response, also as mentioned above.
我如何处理数据回到了JSP的水平。也就是说,一旦我有一个从服务器的响应。
How do I handle the data back at the JSP level. That is, once I have the response from the server.
检查解释上述和给定链路的实例来处理JSP中响应并在一个表中显示该响应数据。
Check the explanation above and the examples of the given links to handle the response in JSP and display the response data in a table.
-
在YouTube上
- DWR基础知识
- DWR basics on YouTube
这篇关于在使用JSP Ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!