SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射(1)
SPRING MVC3 提供了各种映射方式,除了传统的URL方式如(/save?name=value);
又有restful风格的url : /books/1 各种方式任君选择!
本章节内容讲解的代码内容如图:
- SIMPLE标签页对应的代码如下:
<li><a href="#simple">Simple</a></li>
<a id="simpleLink" class="textLink" href="/simple">GET /simple</a>
<a id="simpleRevisited" class="textLink" href="/simple/revisited">GET /simple/revisited</a>
$("a.textLink").click(function(){
var link = $(this);
$.ajax({ url: link.attr("href"), dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
return false;
});
注意一点:这边的@ResponseBody直接返回内容,而不是转向某个视图(jsp页面);
http://127.0.0.1:8010/simple
@Controller
public class SimpleController {
@RequestMapping("/simple")
public @ResponseBody String simple() {
return "Hello world!";
}
}
http://127.0.0.1:8010//simple/revisited
@Controller
public class SimpleControllerRevisited {
@RequestMapping(value="/simple/revisited", method=RequestMethod.GET, headers="Accept=text/plain")
public @ResponseBody String simple() {
return "Hello world revisited!";
}
}
打个标记:晚上继续request mapping的相关代码:
$("#byHeader").click(function(){
var link = $(this);
$.ajax({ url: this.href, dataType: "text", beforeSend: function(req) { req.setRequestHeader("FooHeader", "foo"); }, success: function(form) { MvcUtil.showSuccessResponse(form, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
return false;
});