Java 将搜索字段添加到gson jtable
我试图在我拥有的jtable中的特定列上添加搜索字段。我目前正在使用gson来传递json数据。这是我的控制器:Java 将搜索字段添加到gson jtable,java,json,gson,jquery-jtable,Java,Json,Gson,Jquery Jtable,我试图在我拥有的jtable中的特定列上添加搜索字段。我目前正在使用gson来传递json数据。这是我的控制器: public class gsonTestController extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletRespons
public class gsonTestController extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ( action != null)
{
List<Student> studentList = new ArrayList<Student>();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
response.setContentType("application/json");
if (action.equals("list"))
{
try
{
// Add data to Student list
studentList.add(new Student(1, "Grover", "IT", "xyz@xyz.com"));
studentList.add(new Student(2, "Bugs Bunny", "ECE", "xyz@gmail.com"));
studentList.add(new Student(3, "Taz", "MECH", "abc@gmail.com"));
studentList.add(new Student(4, "Cookie Monster", "ECE", "efg@gmail.com"));
studentList.add(new Student(5, "Billy the Kid", "CSC", "xyz@gmail.com"));
studentList.add(new Student(6, "Dustin Hoffman", "CSC", "123@gmail.com"));
studentList.add(new Student(7, "Obama", "ECE", "789@gmail.com"));
studentList.add(new Student(8, "Adam Sandler", "ECE", "123@gmail.com"));
studentList.add(new Student(9, "Pikachu", "IT", "xyz@gmail.com"));
// Convert Java Object to Json
String jsonArray = gson.toJson(studentList);
//Return Json in the format required by jTable plugin
jsonArray="{\"Result\":\"OK\",\"Records\":"+jsonArray+"}";
System.out.println(jsonArray);
response.getWriter().print(jsonArray);
}
catch(Exception ex){
String error="{\"Result\":\"ERROR\",\"Message\":"+ex.getMessage()+"}";
response.getWriter().print(error);
}
}
}
}
}
公共类gsonTestController扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
public void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
String action=request.getParameter(“action”);
如果(操作!=null)
{
List studentList=new ArrayList();
Gson Gson=new GsonBuilder().setPrettyPrinting().create();
setContentType(“应用程序/json”);
if(action.equals(“列表”))
{
尝试
{
//将数据添加到学生列表
学生列表。添加(新学生(1,“Grover”,“IT”,“IT”)xyz@xyz.com"));
学生列表。添加(新学生)(2,“Bugs Bunny”,“ECE”xyz@gmail.com"));
学生名单。添加(新学生)(3,“Taz”,“机械”,“机械”abc@gmail.com"));
学生列表。添加(新学生(4,“饼干怪兽”,“ECE”,”efg@gmail.com"));
学生列表。添加(新学生)(5,“比利的孩子”,“CSC”xyz@gmail.com"));
学生名单。添加(新学生)(6,“达斯汀·霍夫曼”,“CSC”123@gmail.com"));
学生名单。添加(新学生)(7,“奥巴马”,“欧洲经委会”789@gmail.com"));
学生名单。添加(新学生(8,“亚当·桑德勒”,“欧洲经委会”,“欧洲经委会”)123@gmail.com"));
学生列表。添加(新学生)(9,“皮卡丘”,“IT”,“IT”xyz@gmail.com"));
//将Java对象转换为Json
字符串jsonArray=gson.toJson(studentList);
//以jTable插件所需的格式返回Json
jsonArray=“{\'Result\':\'OK\',\'Records\':“+jsonArray+”}”;
System.out.println(jsonArray);
response.getWriter().print(jsonArray);
}
捕获(例外情况除外){
字符串错误=“{\”结果\“:\”错误\“,\”消息\“:“+ex.getMessage()+”}”;
response.getWriter().print(错误);
}
}
}
}
}
这将访问Student.java(基本getter/setter方法),最后将json数据传递给jsp上的以下脚本:
<script type="text/javascript">
$(document).ready(function() {
$('#StudentTableContainer').jtable({
title : 'Students List',
sorting: true,
defaultSorting: 'Name',
actions : {
listAction: 'gsonTestController?action=list',
createAction:'gsonTestController?action=create',
updateAction: 'gsonTestController?action=update',
deleteAction: 'gsonTestController?action=delete'
},
fields : {
studentId : {
title : 'Student Id',
width : '30%',
key : true,
list : true,
create : true
},
name : {
title : 'Name',
width : '30%',
edit : false
},
department : {
title : 'Department',
width : '30%',
edit : true
},
emailId : {
title : 'Email',
width : '20%',
edit : true
}
}
});
$('#StudentTableContainer').jtable('load');
});
</script>
$(文档).ready(函数(){
$(“#StudentTableContainer”).jtable({
标题:"学生名单",,
排序:对,
defaultSorting:'名称',
行动:{
listAction:'gsonTestController?操作=列表',
createAction:'gsonTestController?action=create',
updateAction:'gsonTestController?操作=更新',
deleteAction:'gsonTestController?操作=删除'
},
字段:{
学生ID:{
标题:“学生Id”,
宽度:“30%”,
关键:没错,
列表:正确,
创建:true
},
姓名:{
标题:“姓名”,
宽度:“30%”,
编辑:false
},
部门:{
标题:"部门",,
宽度:“30%”,
编辑:真
},
emailId:{
标题:“电子邮件”,
宽度:“20%”,
编辑:真
}
}
});
$('#StudentTableContainer').jtable('load');
});
html只是:
<div id="StudentTableContainer"></div>
在阅读有关过滤器的jtable文档时,我找不到任何使用gson实现过滤器的示例代码(与php或ASP.NET等专用服务器端技术相反)。他们的过滤器演示页面位于此处:
现在,我的控制器暂时只使用硬编码数据,因为我还没有设置数据源。最终,所有数据都将通过DAO(在SpringMVC中)进行访问。我不熟悉ASP.NET,但我从页面上的演示中了解到,基本思想是根据用户输入的值设置变量并过滤数据。我只是不确定我将如何在我目前的结构中设置它。我通读了gson用户指南,但我不确定我需要寻找什么。非常感谢任何指针。因此,在他们的过滤器示例中
//Re-load records when user click 'load records' button.
$('#LoadRecordsButton').click(function (e) {
e.preventDefault();
$('#StudentTableContainer').jtable('load', {
name: $('#name').val(),
cityId: $('#cityId').val()
});
});
//Load all records when page is first shown
$('#LoadRecordsButton').click();
您可以看到,他们只是使用用户编写的参数重新执行对URL的请求,因此您可以做的第一件事是使用
String studentName = request.getParameter("studentName");
检查是否通过,并筛选集合studentList
,以仅包括搜索的学生
if (studentName != null) {
Iterator<Student> iterator = studentList.iterator();
while (iterator.hasNext()) {
if (student.getName().startsWith(studentName)) {
iterator.remove();
}
}
}
(其中,LoadRecordsButton
是单击开始搜索的按钮,StudentTableContainer
是表格,name
是输入文本,在其中写入名称)
如果您想手动处理Json数据,然后使用
jTable
的addRecord
,那么可以避免这个代码块,但是在一次小搜索之后,我注意到它有一些问题,所以如果您使用的是spring mvc,那么我会避免它()
$('#LoadRecordsButton').click(function (e) {
e.preventDefault();
$('#StudentTableContainer').jtable('load', {
studentName: $('#name').val()
});
});