1 @RequestMapping(value = "/aaa")//类级别,可以没有
2 public class myController {
3 @RequestMapping(value = "/bbb")//方法级别,必须有
4 public String getMyName() {
5 return "myReturn";
6 }
7 }
1 function login() {//页面异步请求
2 var mydata = \'{"name":"\' + $(\'#name\').val() + \'","id":"\'
3 + $(\'#id\').val() + \'","status":"\' + $(\'#status\').val() + \'"}\';
4 $.ajax({
5 type : \'POST\',
6 contentType : \'application/json\',
7 url : "${pageContext.request.contextPath}/person/login",
8 processData : false,
9 dataType : \'json\',
10 data : mydata,
11 success : function(data) {
12 alert("id: " + data.id + "\\nname: " + data.name + "\\nstatus: "
13 + data.status);
14 },
15 error : function() {
16 alert(\'出错了!\');
17 }
18 });
19 };
1 @RequestMapping(value = "person/login")
2 @ResponseBody
3 public Person login(@RequestBody Person person) {//将请求中的mydata写入Person对象中
4 return person;//不会被解析为跳转路径,而是直接写入HTTP response body中
5 }
1 function profile() {
2 var url = "${pageContext.request.contextPath}/person/profile/";
3 var query = $(\'#id\').val() + \'/\' + $(\'#name\').val() + \'/\'
4 + $(\'#status\').val();
5 url += query;
6 $.get(url, function(data) {
7 alert("id: " + data.id + "\\nname: " + data.name + "\\nstatus: "
8 + data.status);
9 });
10 }
1 @RequestMapping(value = "person/profile/{id}/{name}/{status}")
2 @ResponseBody
3 public Person porfile(@PathVariable int id,@PathVariable String name,@PathVariable boolean status) {
4 return new Person(id, name, status);
5 }
6 //@RequestMapping(value = "/person/profile/{id}/{name}/{status}")中的{id}/{name}/{status}与@PathVariable int id, @PathVariable String name,@PathVariable boolean status一一对应,按名匹配。