2013-11-16 2 views
0

Я хочу вызвать метод в классе POJO в сценарии сетки jquery.Отображение запроса Spring в сетке 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 сетки URL-адрес вызова showEnrolledStudent() метод.

ответ

0

для метода сопоставления с 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(); 
} 

}

0

Если вы хотите вернуть JSON как есть, вы должны использовать аннотацию @ResponseBody. В противном случае DispatcherServlet попытается распознать View, и он не может этого сделать.

@RequestMapping("/showStudent.do") 
@ResponseBody 
public String showEnrolledStudent(Model model){ 
... 
Смежные вопросы