[英]Spring request mapping in jquery grid
我想在jquery网格脚本的POJO类中调用方法。
jQuery("#list2").jqGrid({
datatype: "json",
url:"/showStudent.do",
type:"",
colNames:['Student Name','Age', 'Mobile'],
colModel:[
{name:'name',index:'id', width:55,formatter:custom_func},
{name:'age', width:90},
{name:'mobile', width:100},
],
rowNum:10,
rowList:[10,20,30],
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption:"Student List"
});
因此,以上默认情况下查找get方法,但目标类没有该方法
@RequestMapping("/showStudent.do")
public String showEnrolledStudent(Model model)
{
PersistenceManager pm=PMF.get().getPersistenceManager();
Query query=pm.newQuery(Student.class);
JSONArray studentListJson=new JSONArray();
try
{
List<Student> listOfStudent=(List<Student>) query.execute();
for(Student student:listOfStudent)
{
JSONObject jsonObject=new JSONObject();
jsonObject.put("name", student.getName());
jsonObject.put("age", student.getAge());
jsonObject.put("mobile", student.getMobile());
studentListJson.put(jsonObject);
}
}
catch (Exception e) {
}
System.out.println("hi");
System.out.println(studentListJson);
return studentListJson.toString();
}
那么如何使jquery网格url之间的绑定调用showEnrolledStudent()方法。
如果要按原样返回JSON,则应使用@ResponseBody批注。 否则, DispatcherServlet
会尝试识别View
,而他无法做到这一点。
@RequestMapping("/showStudent.do")
@ResponseBody
public String showEnrolledStudent(Model model){
...
用于具有Http get请求和http Post请求方法属性的请求映射注释的映射方法
@RequestMapping(method=RequestMethod.GET,value="/showStudent.do")
@ResponseBody
public String showEnrolledStudent()
{
//This will get persistence manager for datastore interaction
PersistenceManager pm=PMF.get().getPersistenceManager();
//Query for selecting list of student enrolled
Query query=pm.newQuery(Student.class);
//Json array holds the json objects.
JSONArray studentListJson=new JSONArray();
try
{
List<Student> listOfStudent=(List<Student>) query.execute();
//Loop to create the list of student in json format
for(Student student:listOfStudent)
{
JSONObject jsonObject=new JSONObject();
jsonObject.put("key", student.getKey().getId());
jsonObject.put("name", student.getName());
jsonObject.put("age", student.getAge());
jsonObject.put("mobile", student.getMobile());
studentListJson.put(jsonObject);
}
}
catch (Exception e) {
}
return studentListJson.toString();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.