SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射(1)

本文深入解析Spring MVC 3提供的多种映射方式,包括传统URL方式和RESTful风格的URL。通过示例代码,详细展示了如何实现简单映射、响应映射以及通过HTTP头部进行请求映射。重点介绍了如何使用`@RestController`注解直接返回内容,而非转向视图,以及如何通过Ajax与控制器交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SPRING MVC3 提供了各种映射方式,除了传统的URL方式如(/save?name=value);

又有restful风格的url : /books/1 各种方式任君选择!

 

本章节内容讲解的代码内容如图:

  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;

});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值