spring mvc后台如何处理ajax的请求,并返回json
spring mvc中有一个注解:@ResponseBody,把这个注解放到ajax请求对应的@RequestMapping(xxx)方法上,在方法体内部,把要返回的json赋给一个String类型的变量,然后return这个变量即可,例子如下:
@ResponseBody @RequestMapping("/show") public String show(Map<String,Object> map,String username){ System.out.println("----enter show method"); RepositoryService rs = processEngine.getRepositoryService(); ExecutionService es = processEngine.getExecutionService(); TaskService ts = processEngine.getTaskService(); List<ProcessDefinition> liststemp = rs.createProcessDefinitionQuery().list(); List<PD> lists=toJsonList.processDefine2PD(liststemp) Gson gson=new Gson(); String str=gson.toJson(lists); return str;
@ResponseBody @RequestMapping("/show") public String show(Map<String,Object> map,String username){ System.out.println("----enter show method"); RepositoryService rs = processEngine.getRepositoryService(); ExecutionService es = processEngine.getExecutionService(); TaskService ts = processEngine.getTaskService(); List<ProcessDefinition> liststemp = rs.createProcessDefinitionQuery().list(); List<PD> lists=toJsonList.processDefine2PD(liststemp) Gson gson=new Gson(); String str=gson.toJson(lists); return str;