@RequestMapping(value = "/spittle/{ownerId}",method= {RequestMethod.GET})
public String spittle(@PathVariable String ownerId,@RequestParam("a") int petId,@RequestParam Map allRequestParams) {
System.out.println("uri parameter"+ownerId);//获取的uri中/spittle/{ownerId} ownerid的值 这里为3
System.out.println("request parameter"+petId);//获取的是参数 参数a的值 这里为8
//entrySet返回Set>,而Map.Entry 一个map entry就是一个键值对,getKey拿到key,getValue拿到value
//代替keySet方法返回的是Set,遍历Set后只取到key,再根据key从map中获取value
for(Map.Entry entry:allRequestParams.entrySet()){
System.out.println(entry.getKey()+"->"+entry.getValue());
} //整个map的获取 这里能取到 a->8 b->2
System.out.println(ownerId);
return "spittle_"+ownerId;
}
所以要做到类通配符跳转,value = "/spittle/{ownerId}"这里根据自己的页面跳转进行配置然后return即可。
@RequestMapping(value = "/spittle/{ownerId}",method= {RequestMethod.POST})
如果只限定了POST方法,然后通过GET请求是会报405错误的。
@RequestMapping(value = "/spittle2",method= {RequestMethod.POST,RequestMethod.GET})
public ModelAndView ListData2() throws Exception {
ModelAndView model = new ModelAndView("ok");
String json = "{\"total\":10,\"rows\":[{\"a\":1,\"b\":\"ee\"}]}";
model.addObject("msg", json);
return model;
}
或者直接写成:
@RequestMapping(value = "/spittle2",method= {RequestMethod.POST,RequestMethod.GET})
public ModelAndView ListData2() throws Exception {
return new ModelAndView("ok","msg", "{\"total\":10,\"rows\":[{\"a\":1,\"b\":\"ee\"}]}");
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
${msg}
@Controller
public class HomeController {
@RequestMapping(value = "/test")
public String helloWorld() {
return "HelloWorld";
}
}
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@ResponseBody
@RequestMapping(value = "/test")
public String helloWorld() {
return "Hello World";
}
http://localhost:8080/project/static/test.html:
ajax交互
@RequestMapping(value = "/spittle",method= {RequestMethod.POST,RequestMethod.GET})
public @ResponseBody Map ListData() throws Exception {
Map map = new HashMap();
map.put("a", "a");
map.put("b", 2);
return map;
}
com.fasterxml.jackson.core
jackson-databind
2.4.0
String json = "[{'id':1,'name':'ee','password':'1'}]";
String json = "[{\"id\":1,\"name\":\"ee\",\"password\":\"1\"}]";
@RequestMapping(value = "/spittle",method= {RequestMethod.POST,RequestMethod.GET})
public @ResponseBody Map ListData() throws Exception {
Map map = new HashMap();
map.put("a", 1);
map.put("b", 2);
return map;
}
@RequestMapping(value = "/spittle",method= {RequestMethod.POST,RequestMethod.GET})
public @ResponseBody Map ListData() throws Exception {
Map bigmap = new HashMap();
Map map1 = new HashMap();
Map map2 = new HashMap();
bigmap.put("total", "13");
map1.put("a", 1);
map2.put("a", 1);
map1.put("b", 2);
map2.put("b", 2);
Map[] maparray = {map1,map2};
bigmap.put("rows",maparray);
return bigmap;
}
Document