Я хочу вызвать метод в классе POJO в сценарии сетки jquery.
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 grid url для метода showEnrolledStudent().
для метода сопоставления с запросом на получение Http и используется свойство метода HTTP Post request для аннотации сопоставления запроса
@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();
}
}
Если вы хотите вернуть JSON как есть, вы должны использовать аннотацию @ResponseBody. В противном случае DispatcherServlet
попытается распознать View
, и он не сможет этого сделать.
@RequestMapping("/showStudent.do")
@ResponseBody
public String showEnrolledStudent(Model model){
...