在这篇文章中,你将会看到 @RequestMapping 注解在被用来进行 Spring MVC 控制器方法的映射可以如何发挥其多才多艺的功能的。
Request Mapping 基础用法
在 Spring MVC 应用程序中,RequestDispatcher (在 Front Controller 之下) 这个 servlet 负责将进入的 HTTP 请求路由到控制器的处理方法。
在对 Spring MVC 进行的配置的时候, 你需要指定请求与处理方法之间的映射关系。
要配置 Web 请求的映射,就需要你用上 @RequestMapping 注解。
@RequestMapping 注解可以在控制器类的级别和/或其中的方法的级别上使用。
在类的级别上的注解会将一个特定请求或者请求模式映射到一个控制器之上。之后你还可以另外添加方法级别的注解来进一步指定到处理方法的映射关系。
下面是一个同时在类和方法上应用了 @RequestMapping 注解的示例:
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping("/")
- String get() {
- //mapped to hostname:port/home/
- return “Hello from get”;
- }
- @RequestMapping("/index")
- String index() {
- //mapped to hostname:port/home/index/
- return “Hello from index”;
- }
- }
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping("/")
String get() {
//mapped to hostname:port/home/
return “Hello from get”;
}
@RequestMapping("/index")
String index() {
//mapped to hostname:port/home/index/
return “Hello from index”;
}
}
如上述代码所示,到 /home 的请求会由 get() 方法来处理,而到 /home/index 的请求会由 index() 来处理。
@RequestMapping 来处理多个 URI
你可以将多个请求映射到一个方法上去,只需要添加一个带有请求路径值列表的 @RequestMapping 注解就行了。
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = {
- “”,
- “/page”,
- “page*”,
- “view/*,**/msg”
- })
- String indexMultipleMapping() {
- return “Hello from index multiple mapping.”;
- }
- }
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = {
"",
"/page",
"page*",
"view/*,**/msg"
})
String indexMultipleMapping() {
return "Hello from index multiple mapping.";
}
}
如你在这段代码中所看到的,@RequestMapping 支持统配符以及ANT风格的路径。前面这段代码中,如下的这些 URL 都会由 indexMultipleMapping() 来处理:
- localhost:8080/home
- localhost:8080/home/
- localhost:8080/home/page
- localhost:8080/home/pageabc
- localhost:8080/home/view/
- localhost:8080/home/view/view
- 带有 @RequestParam 的 @RequestMapping
@RequestParam 注解配合 @RequestMapping 一起使用,可以将请求的参数同处理方法的参数绑定在一起。
@RequestParam 注解使用的时候可以有一个值,也可以没有值。这个值指定了需要被映射到处理方法参数的请求参数, 代码如下所示:
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = "/id")
- String getIdByValue(@RequestParam("id") String personId) {
- System.out.println("ID is " + personId);
- return "Get ID from query string of URL with value element";
- }
- @RequestMapping(value = "/personId")
- String getId(@RequestParam String personId) {
- System.out.println("ID is " + personId);
- return "Get ID from query string of URL without value element";
- }
- }
@RestController @RequestMapping("/home") public class IndexController {
@RequestMapping(value = "/id") String getIdByValue(@RequestParam("id") String personId) { System.out.println("ID is " + personId); return "Get ID from query string of URL with value element"; } @RequestMapping(value = "/personId") String getId(@RequestParam String personId) { System.out.println("ID is " + personId); return "Get ID from query string of URL without value element"; }
}
在代码的第6行,id 这个请求参数被映射到了 thegetIdByValue() 这个处理方法的参数 personId 上。
如果请求参数和处理方法参数的名称一样的话,@RequestParam 注解的 value 这个参数就可省掉了, 如代码的第11行所示。
@RequestParam 注解的 required 这个参数定义了参数值是否是必须要传的。
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = “/name”)
- String getName(@RequestParam(value = “person”, required = false) String personName) {
- return “Required element of request param”;
- }
- }
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = “/name”)
String getName(@RequestParam(value = “person”, required = false) String personName) {
return “Required element of request param”;
}
}
在这段代码中,因为 required 被指定为 false,所以 getName() 处理方法对于如下两个 URL 都会进行处理:
- /home/name?person=xyz
- /home/name
- @RequestParam 的 defaultValue 取值就是用来给取值为空的请求参数提供一个默认值的。
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = "/name")
- String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {
- return "Required element of request param";
- }
- }
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/name") String getName(@RequestParam(value = "person", defaultValue = "John") String personName) { return "Required element of request param"; } }
在这段代码中,如果 person 这个请求参数为空,那么 getName() 处理方法就会接收 John 这个默认值作为其参数。
用 @RequestMapping 处理 HTTP 的各种方法
Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
所有的请求默认都会是 HTTP GET 类型的。
为了能降一个请求映射到一个特定的 HTTP 方法,你需要在 @RequestMapping 中使用 method 来声明 HTTP 请求所使用的方法类型,如下所示:
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(method = RequestMethod.GET)
- String get() {
- return "Hello from get";
- }
- @RequestMapping(method = RequestMethod.DELETE)
- String delete() {
- return "Hello from delete";
- }
- @RequestMapping(method = RequestMethod.POST)
- String post() {
- return "Hello from post";
- }
- @RequestMapping(method = RequestMethod.PUT)
- String put() {
- return "Hello from put";
- }
- @RequestMapping(method = RequestMethod.PATCH)
- String patch() {
- return "Hello from patch";
- }
- }
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(method = RequestMethod.GET) String get() { return "Hello from get"; } @RequestMapping(method = RequestMethod.DELETE) String delete() { return "Hello from delete"; } @RequestMapping(method = RequestMethod.POST) String post() { return "Hello from post"; } @RequestMapping(method = RequestMethod.PUT) String put() { return "Hello from put"; } @RequestMapping(method = RequestMethod.PATCH) String patch() { return "Hello from patch"; } }
在上述这段代码中, @RequestMapping 注解中的 method 元素声明了 HTTP 请求的 HTTP 方法的类型。
所有的处理处理方法会处理从这同一个 URL( /home)进来的请求, 但要看指定的 HTTP 方法是什么来决定用哪个方法来处理。
例如,一个 POST 类型的请求 /home 会交给 post() 方法来处理,而一个 DELETE 类型的请求 /home 则会由 delete() 方法来处理。
你会看到 Spring MVC 将使用这样相同的逻辑来映射其它的方法。
用 @RequestMapping 来处理生产和消费对象
可以使用 @RequestMapping 注解的 produces 和 consumes 这两个元素来缩小请求映射类型的范围。
为了能用请求的媒体类型来产生对象, 你要用到 @RequestMapping 的 produces 元素再结合着 @ResponseBody 注解。
你也可以利用 @RequestMapping 的 comsumes 元素再结合着 @RequestBody 注解用请求的媒体类型来消费对象。
下面这段代码就用到的 @RequestMapping 的生产和消费对象元素:
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = "/prod", produces = {
- "application/JSON"
- })
- @ResponseBody
- String getProduces() {
- return "Produces attribute";
- }
- @RequestMapping(value = "/cons", consumes = {
- "application/JSON",
- "application/XML"
- })
- String getConsumes() {
- return "Consumes attribute";
- }
- }
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/prod", produces = { "application/JSON" }) @ResponseBody String getProduces() { return "Produces attribute"; }
@RequestMapping(value = "/cons", consumes = { "application/JSON", "application/XML" }) String getConsumes() { return "Consumes attribute"; }
}
在这段代码中, getProduces() 处理方法会产生一个 JSON 响应, getConsumes() 处理方法可以同时处理请求中的 JSON 和 XML 内容。
使用 @RequestMapping 来处理消息头
@RequestMapping 注解提供了一个 header 元素来根据请求中的消息头内容缩小请求映射的范围。
在可以指定 header 元素的值,用 myHeader = myValue 这样的格式:
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = “/head”, headers = {
- “content-type=text/plain”
- })
- String post() {
- return “Mapping applied along with headers”;
- }
- }
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = “/head”, headers = {
“content-type=text/plain”
})
String post() {
return “Mapping applied along with headers”;
}
}
在上面这段代码中, @RequestMapping 注解的 headers 属性将映射范围缩小到了 post() 方法。有了这个,post() 方法就只会处理到 /home/head 并且 content-typeheader 被指定为 text/plain 这个值的请求。
你也可以像下面这样指定多个消息头:
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = “/head”, headers = {
- “content-type=text/plain”,
- “content-type=text/html”
- }) String post() {
- return “Mapping applied along with headers”;
- }
- }
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = “/head”, headers = {
“content-type=text/plain”,
“content-type=text/html”
}) String post() {
return “Mapping applied along with headers”;
}
}
这样, post() 方法就能同时接受 text/plain 还有 text/html 的请求了。
使用 @RequestMapping 来处理请求参数
@RequestMapping 直接的 params 元素可以进一步帮助我们缩小请求映射的定位范围。使用 params 元素,你可以让多个处理方法处理到同一个URL 的请求, 而这些请求的参数是不一样的。
你可以用 myParams = myValue 这种格式来定义参数,也可以使用通配符来指定特定的参数值在请求中是不受支持的。
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @RequestMapping(value = “/fetch”, params = {
- “personId=10”
- })
- String getParams(@RequestParam(“personId”) String id) {
- return “Fetched parameter using params attribute = " + id;
- }
- @RequestMapping(value = “/fetch”, params = {
- “personId=20”
- })
- String getParamsDifferent(@RequestParam(“personId”) String id) {
- return “Fetched parameter using params attribute = " + id;
- }
- }
@RestController
@RequestMapping(”/home”)
public class IndexController {
@RequestMapping(value = “/fetch”, params = {
“personId=10”
})
String getParams(@RequestParam(“personId”) String id) {
return “Fetched parameter using params attribute = " + id;
}
@RequestMapping(value = “/fetch”, params = {
“personId=20”
})
String getParamsDifferent(@RequestParam(“personId”) String id) {
return “Fetched parameter using params attribute = " + id;
}
}
在这段代码中,getParams() 和 getParamsDifferent() 两个方法都能处理相同的一个 URL (/home/fetch) ,但是会根据 params 元素的配置不同而决定具体来执行哪一个方法。
例如,当 URL 是 /home/fetch?id=10 的时候, getParams() 会执行,因为 id 的值是10,。对于 localhost:8080/home/fetch?personId=20 这个URL, getParamsDifferent() 处理方法会得到执行,因为 id 值是 20。
使用 @RequestMapping 处理动态 URI
@RequestMapping 注解可以同 @PathVaraible 注解一起使用,用来处理动态的 URI,URI 的值可以作为控制器中处理方法的参数。你也可以使用正则表达式来只处理可以匹配到正则表达式的动态 URI。
- @RestController
- @RequestMapping(”/home”)
- public class IndexController {
- @RequestMapping(value = “/fetch/{id}”, method = RequestMethod.GET)
- String getDynamicUriValue(@PathVariable String id) {
- System.out.println(“ID is " + id);
- return “Dynamic URI parameter fetched”;
- }
- @RequestMapping(value = “/fetch/{id:[a-z]+}/{name}”, method = RequestMethod.GET)
- String getDynamicUriValueRegex(@PathVariable(“name”) String name) {
- System.out.println(“Name is " + name);
- return “Dynamic URI parameter fetched using regex”;
- }
- }
@RestController
@RequestMapping(”/home”)
public class IndexController {
@RequestMapping(value = “/fetch/{id}”, method = RequestMethod.GET)
String getDynamicUriValue(@PathVariable String id) {
System.out.println(“ID is " + id);
return “Dynamic URI parameter fetched”;
}
@RequestMapping(value = “/fetch/{id:[a-z]+}/{name}”, method = RequestMethod.GET)
String getDynamicUriValueRegex(@PathVariable(“name”) String name) {
System.out.println(“Name is " + name);
return “Dynamic URI parameter fetched using regex”;
}
}
在这段代码中,方法 getDynamicUriValue() 会在发起到 localhost:8080/home/fetch/10 的请求时执行。这里 getDynamicUriValue() 方法 id 参数也会动态地被填充为 10 这个值。
方法 getDynamicUriValueRegex() 会在发起到 localhost:8080/home/fetch/category/shirt 的请求时执行。不过,如果发起的请求是 /home/fetch/10/shirt 的话,会抛出异常,因为这个URI并不能匹配正则表达式。
@PathVariable 同 @RequestParam的运行方式不同。你使用 @PathVariable 是为了从 URI 里取到查询参数值。换言之,你使用 @RequestParam 是为了从 URI 模板中获取参数值。
@RequestMapping 默认的处理方法
在控制器类中,你可以有一个默认的处理方法,它可以在有一个向默认 URI 发起的请求时被执行。
下面是默认处理方法的示例:
- @RestController
- @RequestMapping(”/home”)
- public class IndexController {
- @RequestMapping()
- String
- default () {
- return “This is a default method for the class”;
- }
- }
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping()
String
default () {
return “This is a default method for the class”;
}
}
在这段代码中,向 /home 发起的一个请求将会由 default() 来处理,因为注解并没有指定任何值。
@RequestMapping 快捷方式
Spring 4.3 引入了方法级注解的变体,也被叫做 @RequestMapping 的组合注解。组合注解可以更好的表达被注解方法的语义。它们所扮演的角色就是针对 @RequestMapping 的封装,而且成了定义端点的标准方法。
例如,@GetMapping 是一个组合注解,它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。
方法级别的注解变体有如下几个:
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping
- 如下代码展示了如何使用组合注解:
- @RestController
- @RequestMapping("/home")
- public class IndexController {
- @GetMapping("/person")
- public @ResponseBody ResponseEntity < String > getPerson() {
- return new ResponseEntity < String > ("Response from GET", HttpStatus.OK);
- }
- @GetMapping("/person/{id}")
- public @ResponseBody ResponseEntity < String > getPersonById(@PathVariable String id) {
- return new ResponseEntity < String > ("Response from GET with id " + id, HttpStatus.OK);
- }
- @PostMapping("/person")
- public @ResponseBody ResponseEntity < String > postPerson() {
- return new ResponseEntity < String > ("Response from POST method", HttpStatus.OK);
- }
- @PutMapping("/person")
- public @ResponseBody ResponseEntity < String > putPerson() {
- return new ResponseEntity < String > ("Response from PUT method", HttpStatus.OK);
- }
- @DeleteMapping("/person")
- public @ResponseBody ResponseEntity < String > deletePerson() {
- return new ResponseEntity < String > ("Response from DELETE method", HttpStatus.OK);
- }
- @PatchMapping("/person")
- public @ResponseBody ResponseEntity < String > patchPerson() {
- return new ResponseEntity < String > ("Response from PATCH method", HttpStatus.OK);
- }
- }
@RestController @RequestMapping("/home") public class IndexController { @GetMapping("/person") public @ResponseBody ResponseEntity < String > getPerson() { return new ResponseEntity < String > ("Response from GET", HttpStatus.OK); } @GetMapping("/person/{id}") public @ResponseBody ResponseEntity < String > getPersonById(@PathVariable String id) { return new ResponseEntity < String > ("Response from GET with id " + id, HttpStatus.OK); } @PostMapping("/person") public @ResponseBody ResponseEntity < String > postPerson() { return new ResponseEntity < String > ("Response from POST method", HttpStatus.OK); } @PutMapping("/person") public @ResponseBody ResponseEntity < String > putPerson() { return new ResponseEntity < String > ("Response from PUT method", HttpStatus.OK); } @DeleteMapping("/person") public @ResponseBody ResponseEntity < String > deletePerson() { return new ResponseEntity < String > ("Response from DELETE method", HttpStatus.OK); } @PatchMapping("/person") public @ResponseBody ResponseEntity < String > patchPerson() { return new ResponseEntity < String > ("Response from PATCH method", HttpStatus.OK); } }
在这段代码中,每一个处理方法都使用 @RequestMapping 的组合变体进行了注解。尽管每个变体都可以使用带有方法属性的 @RequestMapping 注解来互换实现, 但组合变体仍然是一种最佳的实践 — 这主要是因为组合注解减少了在应用程序上要配置的元数据,并且代码也更易读。
@RequestMapping 总结
如你在本文中所看到的,@RequestMapping 注解是非常灵活的。你可以使用该注解配置 Spring MVC 来处理大量的场景用例。它可以被用来在 Spring MVC 中配置传统的网页请求,也可以是 REST 风格的 Web 服务。
英文原文: Using the Spring @RequestMapping Annotation<div id="news_recommended_n2"> <ins data-revive-zoneid="163" data-revive-id="8c38e720de1c90a6f6ff52f3f89c4d57" id="revive-0-0"></ins> </div>
<div> 来自: <a href="https://www.oschina.net/translate/using-the-spring-requestmapping-annotation?lang=chs&page=1#">oschina</a> </div>
<div id="digg_bottom" class="clearfix"> <div class="share_weibo" id="">分享到: <a data-type="sina" href="javascript:;" title="分享到新浪微博"><img src="/images/sina.jpg"></a> <a data-type="qq" href="javascript:;" title="分享到腾讯微博"><img src="/images/tec.jpg"></a> </div>
4
顶0
踩<div style="text-align: center"> <div id="kp_box_400" data-pid="400" data-track-view="{"mod":"kp_popu_400-515","con":",,"}" data-track-click="{"mod":"kp_popu_400-515","con":",,"}"><div><iframe width="655" frameborder="0" height="80" scrolling="no" src="//pos.baidu.com/s?hei=80&wid=655&di=u3559370&ltu=https%3A%2F%2Fwww.iteye.com%2Fnews%2F32657%2F&psi=e8b17b049f780fb3a85be27b94feda0f&dai=2&tpr=1545218839007&cec=UTF-8&ant=0&ti=%E8%B6%85%E8%AF%A6%E7%BB%86%20Spring%20%40RequestMapping%20%E6%B3%A8%E8%A7%A3%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7%20-%20%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80%20-%20ITeye%E8%B5%84%E8%AE%AF&tlm=1545218839&drs=1&ari=2&cfv=0&dis=0&cmi=4&exps=111000,113000,110010&par=1280x680&ccd=24&pcs=1019x573&pss=1019x11060&cdo=-1&cja=false&dc=3&dri=0&chi=3&cpl=3&col=zh-CN&dtm=HTML_POST&psr=1280x720&prot=2&pis=-1x-1&tcn=1545218839&cce=true&ps=8738x12"></iframe></div><script type="text/javascript" src="//rabc1.iteye.com/source/0jla.js?kfxcc=nxgl"></script></div> </div>
发表评论
相关推荐
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/u010429286/article/details/77973336" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/u010429286/article/details/77973336,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/u010429286/article/details/77973336,-"}" target="_blank" title="超详细 Spring @RequestMapping 注解使用技巧">超<em class="related_suggestion_highlight">详细</em> <em class="related_suggestion_highlight">Spring</em> @<em class="related_suggestion_highlight">RequestMapping</em> <em class="related_suggestion_highlight">注解</em><em class="related_suggestion_highlight">使用</em><em class="related_suggestion_highlight">技巧</em></a> <p> 转载自:https://www.oschina.net/translate/using-the-<em class="related_suggestion_highlight">spring</em>-<em class="related_suggestion_highlight">requestmapping</em>-annotation
@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一。这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。在这篇文章中,你将会看到 @RequestMapping 注
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/yaerfeng/article/details/52593128" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/yaerfeng/article/details/52593128,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/yaerfeng/article/details/52593128,-"}" target="_blank" title="springmvc @RequestMapping注解的入门配置教程"><em class="related_suggestion_highlight">spring</em>mvc @<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em>的入门配置教程</a> <p> 原文:<em class="related_suggestion_highlight">spring</em>mvc
@RequestMapping注解的入门配置教程
源代码下载地址:http://www.zuidaima.com/share/1837785387207680.htm
直接代码喽。
@Controller
@RequestMapping("/appointments")
public class AppointmentsController
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/wwwihpccn/article/details/52801354" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/wwwihpccn/article/details/52801354,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/wwwihpccn/article/details/52801354,-"}" target="_blank" title="Spring 4.3 新增加的请求注解,对@RequestMapping()进行了细分"><em class="related_suggestion_highlight">Spring</em> 4.3 新增加的请求<em class="related_suggestion_highlight">注解</em>,对@<em class="related_suggestion_highlight">RequestMapping</em>()进行了细分</a> <p> <em class="related_suggestion_highlight">Spring</em> 4.3 新增加的请求<em class="related_suggestion_highlight">注解</em>,对@<em class="related_suggestion_highlight">RequestMapping</em>()进行了细分
Spring 4.3针对注册请求增加了5个新注册分别如下:
1)、@GetMapping 相当于Get请求(@RequestMapping(method=RequestMethod.GET))2)、@PostMapping 相当于POST请求 (@RequestMapping(method=
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/linzhiqiang0316/article/details/52316606" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/linzhiqiang0316/article/details/52316606,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/linzhiqiang0316/article/details/52316606,-"}" target="_blank" title="Spring MVC中常用注解之RequestMapping详解"><em class="related_suggestion_highlight">Spring</em> MVC中常用<em class="related_suggestion_highlight">注解</em>之<em class="related_suggestion_highlight">RequestMapping</em>详解</a> <p> <em class="related_suggestion_highlight">Spring</em>MVC和Struts2最大区别可能就是<em class="related_suggestion_highlight">注解</em>的<em class="related_suggestion_highlight">使用</em>。因为<em class="related_suggestion_highlight">Spring</em>MVC中可以实现在类这个层面上面配置信息,也可以在方法层面上面配置信息,既灵活又方便,不需要写大量的配置文件,也不需要写大量的action控制类,因为<em class="related_suggestion_highlight">Spring</em>MVC仅仅通过<em class="related_suggestion_highlight">注解</em>就可以实现了Action控制器的跳转和一些常用的功能。
通过注解来实现Action控制器中的execute方法和页面跳转的功能,而且Spri
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/sunsiwenvip/article/details/80230050" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/sunsiwenvip/article/details/80230050,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/sunsiwenvip/article/details/80230050,-"}" target="_blank" title="Spring是怎样检测并处理我们@RequestMapping注解的:"><em class="related_suggestion_highlight">Spring</em>是怎样检测并处理我们@<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em>的:</a> <p> BeanFactory在创建Bean过程中需要执行populateBean方法,populateBean方法便是@Autowired<em class="related_suggestion_highlight">注解</em>的处理过程,执行的属性的自动注入等操作。其中InitiaizingBean接口,它的实现Bean会在容器完成属性注入后执行一个自定义操作 &lt;mvc:annotation-driven/&gt;当在配置文件中加上该标记后,<em class="related_suggestion_highlight">Spring</em>(3.1后)会默认为我们... </p> </li> <li class="news-recommends-ajax"> <a href="https://blog.csdn.net/suyu_yuan/article/details/54343198" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/suyu_yuan/article/details/54343198,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/suyu_yuan/article/details/54343198,-"}" target="_blank" title="Spring MVC的@RequestMapping注解类的params参数用法示例"><em class="related_suggestion_highlight">Spring</em> MVC的@<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em>类的params参数用法示例</a> <p> 例子如下:
1、前端请求(extjs3的写法):
Ext.Ajax.request({
url: ‘changeResidenceServlet?action=zhifa’,
method: ‘POST’,
params: {
pk_id: _pk_id,
ZFDDYJ:sfsshyj
} ,
callback: function (options, success, orig
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/hexiaodiao/article/details/82562817" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/hexiaodiao/article/details/82562817,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/hexiaodiao/article/details/82562817,-"}" target="_blank" title="Spring Boot类上注解@RequestMapping失效访问不到页面"><em class="related_suggestion_highlight">Spring</em> Boot类上<em class="related_suggestion_highlight">注解</em>@<em class="related_suggestion_highlight">RequestMapping</em>失效访问不到页面</a> <p> 今天学习<em class="related_suggestion_highlight">Spring</em>Boot的时候,在类上@<em class="related_suggestion_highlight">RequestMapping</em>,访问地址时发现无法访问.原来是
spring.mvc.view.prefix=/WEB-INF/jsp/
之前配置的WEB-INF前面没有/ 记录一下踩过的坑…
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/xiangwanpeng/article/details/53053140" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/xiangwanpeng/article/details/53053140,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/xiangwanpeng/article/details/53053140,-"}" target="_blank" title="SpringMVC使用注解@RequestMapping映射请求"><em class="related_suggestion_highlight">Spring</em>MVC<em class="related_suggestion_highlight">使用</em><em class="related_suggestion_highlight">注解</em>@<em class="related_suggestion_highlight">RequestMapping</em>映射请求</a> <p> <em class="related_suggestion_highlight">Spring</em>MVC通过<em class="related_suggestion_highlight">使用</em>@<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em>,实现指定控制器可以处理哪些URL请求。控制器的类定义及方法定义处都可以标注@<em class="related_suggestion_highlight">RequestMapping</em>:
类定义处:提供初步的请求映射信息。相对于WEB 应用的根目录。
方法定义处:提供进一步的细分映射信息。相对于类定义处的URL。若类定义处未标注@RequestMapping,则方法处标记的URL 相对于WEB 应用的根目录。 下面
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/u012557814/article/details/72549386" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/u012557814/article/details/72549386,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/u012557814/article/details/72549386,-"}" target="_blank" title="模拟SpringMVC简单实现注解@RequestMapping">模拟<em class="related_suggestion_highlight">Spring</em>MVC简单实现<em class="related_suggestion_highlight">注解</em>@<em class="related_suggestion_highlight">RequestMapping</em></a> <p> 根据自己的简单模拟<em class="related_suggestion_highlight">注解</em>@<em class="related_suggestion_highlight">RequestMapping</em>的实现方式import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import org.springframework.core.LocalVariableTableParameterNa
<li class="news-recommends-ajax"> <a href="https://download.csdn.net/download/gulanga5/9860162" data-track-click="{"mod":"popu_712","con":",https://download.csdn.net/download/gulanga5/9860162,-"}" data-track-view="{"mod":"popu_712","con":",https://download.csdn.net/download/gulanga5/9860162,-"}" target="_blank" title="框架Spring的Controller注解和RequestMapping注解 在MVC应用注意事项">框架<em class="related_suggestion_highlight">Spring</em>的Controller<em class="related_suggestion_highlight">注解</em>和<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em> 在MVC应用注意事项</a> <p> <em class="related_suggestion_highlight">Spring</em>MVC不能只<em class="related_suggestion_highlight">使用</em>@Controller而不<em class="related_suggestion_highlight">使用</em>@<em class="related_suggestion_highlight">RequestMapping</em> </p> </li> <li class="news-recommends-ajax"> <a href="https://blog.csdn.net/jintianhen1/article/details/29575209" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/jintianhen1/article/details/29575209,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/jintianhen1/article/details/29575209,-"}" target="_blank" title="@requestmapping--springmvc注解的使用变量和全局部*替代变量方法">@<em class="related_suggestion_highlight">requestmapping</em>--<em class="related_suggestion_highlight">spring</em>mvc<em class="related_suggestion_highlight">注解</em>的<em class="related_suggestion_highlight">使用</em>变量和全局部*替代变量方法</a> <p> package com.wanju.project001.zonghe.common.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Con
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/wxhcr912/article/details/78907686" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/wxhcr912/article/details/78907686,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/wxhcr912/article/details/78907686,-"}" target="_blank" title="SpringMVC学习(1)--SpringMVC的第一个HelloWorld及@RequestMapping注解的基本使用"><em class="related_suggestion_highlight">Spring</em>MVC学习(1)--<em class="related_suggestion_highlight">Spring</em>MVC的第一个HelloWorld及@<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em>的基本<em class="related_suggestion_highlight">使用</em></a> <p> 一、<em class="related_suggestion_highlight">Spring</em>MVC的第一个HelloWorld
Spring MVC主要由DispatcherServlet、处理器映射、处理器(控制器)、视图解析器、视图组成。他的两个核心是:
处理器映射:选择使用哪个控制器来处理请求
视图解析器:选择结果应该如何渲染1.导入基本所需jar包:
2.在Web.xml配置文件中配置SpringMVC
<we <="" p="">
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/evilcry2012/article/details/50722478" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/evilcry2012/article/details/50722478,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/evilcry2012/article/details/50722478,-"}" target="_blank" title="spring是怎么知道哪个方法或者类上有@requestMapping这个注解"><em class="related_suggestion_highlight">spring</em>是怎么知道哪个方法或者类上有@<em class="related_suggestion_highlight">requestMapping</em>这个<em class="related_suggestion_highlight">注解</em></a> <p> <em class="related_suggestion_highlight">spring</em> mvc <em class="related_suggestion_highlight">注解</em> @<em class="related_suggestion_highlight">requestMapping</em>
分享| 2011-12-24
13:17q138026310 | 浏览
36240 次 悬赏:20编程语言
spring是怎么知道哪个方法或者类上有@requestMapping这个注解专业回答
jfq721
2012-01-07 23:12靠spring内部的相应处理类了
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/yerenyuan_pku/article/details/72511749" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/yerenyuan_pku/article/details/72511749,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/yerenyuan_pku/article/details/72511749,-"}" target="_blank" title="SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解"><em class="related_suggestion_highlight">Spring</em>MVC学习(六)——<em class="related_suggestion_highlight">Spring</em>MVC高级参数绑定与@<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em></a> <p> 高级参数绑定现在进入<em class="related_suggestion_highlight">Spring</em>MVC高级参数绑定的学习,本文所有案例代码的编写均建立在前文<em class="related_suggestion_highlight">Spring</em>MVC学习(五)——<em class="related_suggestion_highlight">Spring</em>MVC的参数绑定的案例基础之上,因此希望读者能仔细阅读这篇文章。绑定数组现有这样一个需求:在商品列表页面选中多个商品,然后删除之。下面是我对该需求的分析:此功能要求商品列表页面中的每个商品前有一个checkbook(复选框),选中多个商品后点击删除按钮把商品id传递 </p> </li> <li class="news-recommends-ajax"> <a href="https://blog.csdn.net/jaryle/article/details/54342835" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/jaryle/article/details/54342835,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/jaryle/article/details/54342835,-"}" target="_blank" title="spring3支持@value注解从配置文件中读取配置值,大大简化读取配置文件的代码@"><em class="related_suggestion_highlight">spring</em>3支持@value<em class="related_suggestion_highlight">注解</em>从配置文件中读取配置值,大大简化读取配置文件的代码@</a> <p> @value从配置文件properties中读取配置值
@Controller
@RequestMapping("/value")
public class ValuePropertyController extends ApplicationController{@Value("#{configProperties['jdbc.jdbcUrl']}") </p> </li> <li class="news-recommends-ajax"> <a href="https://blog.csdn.net/fangdengfu123/article/details/77988751" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/fangdengfu123/article/details/77988751,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/fangdengfu123/article/details/77988751,-"}" target="_blank" title="@RequestMapping注解使用技巧">@<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em><em class="related_suggestion_highlight">使用</em><em class="related_suggestion_highlight">技巧</em></a> <p> @<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em><em class="related_suggestion_highlight">使用</em><em class="related_suggestion_highlight">技巧</em>@<em class="related_suggestion_highlight">RequestMapping</em>基础用法
@RequestMapping(“/index”)@RequestMapping处理多个URL
@RequestMapping(value={“”,”/page”,”/page*”,”/view/”,”*/msg”})带有@RequestMapping的@RequestMapping
@RequestMappin
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/menghuanzhiming/article/details/79282805" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/menghuanzhiming/article/details/79282805,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/menghuanzhiming/article/details/79282805,-"}" target="_blank" title="springMVC请求注解@RequestMapping各个属性值"><em class="related_suggestion_highlight">spring</em>MVC请求<em class="related_suggestion_highlight">注解</em>@<em class="related_suggestion_highlight">RequestMapping</em>各个属性值</a> <p> 1、<em class="related_suggestion_highlight">RequestMapping</em>接口的源码如下,里面定义了七个属性
(1)@Target中有两个属性,分别为 ElementType.METHOD 和 ElementType.TYPE ,也就是说 @RequestMapping 可以在方法和类的声明中使用
(2)可以看到注解中的属性除了 name() 返回的字符串,其它的方法均返回数组,也就是可以定义多个属性值,例如 value()可以同时定…
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/hdfyq/article/details/29439093" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/hdfyq/article/details/29439093,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/hdfyq/article/details/29439093,-"}" target="_blank" title="eclipse导出jar包,不能加载@RequestMapping等类似注解的问题">eclipse导出jar包,不能加载@<em class="related_suggestion_highlight">RequestMapping</em>等类似<em class="related_suggestion_highlight">注解</em>的问题</a> <p> 【缘由】开发平台的一些关键代码不能提交给客户, 打包成jar </p> </li> <li class="news-recommends-ajax"> <a href="https://blog.csdn.net/cacacai/article/details/81626285" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/cacacai/article/details/81626285,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/cacacai/article/details/81626285,-"}" target="_blank" title="Spring Boot中@RequestMapping 用法详解之地址映射(转)"><em class="related_suggestion_highlight">Spring</em> Boot中@<em class="related_suggestion_highlight">RequestMapping</em> 用法详解之地址映射(转)</a> <p> 转载自http://www.cnblogs.com/qq78292959/p/3760560.html
引言
前段时间使用springboot来开发项目,并且需要使用到传输JSON数据,并且踩了很多坑,无意中找到了这篇文章,详细的说明了@RequestMapping的使用
引言
简介:
1、 value, method;
2、 consumes,produces;
3、 para…
<li class="news-recommends-ajax"> <a href="https://blog.csdn.net/qq_32286125/article/details/75800903" data-track-click="{"mod":"popu_712","con":",https://blog.csdn.net/qq_32286125/article/details/75800903,-"}" data-track-view="{"mod":"popu_712","con":",https://blog.csdn.net/qq_32286125/article/details/75800903,-"}" target="_blank" title="@RequestMapping注解内的value的值不能含有manager,否则会报404错误">@<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em>内的value的值不能含有manager,否则会报404错误</a> <p> @<em class="related_suggestion_highlight">RequestMapping</em><em class="related_suggestion_highlight">注解</em>内的value的值不能含有manager,否则会报404错误 </p> </li> </ul> </div> <script> setTimeout(function () { not_loading = true; pageNum = 3; oList = jQuery("#album_detail_wrap"); jQuery(window).scroll(function() { var scrollTop = jQuery(this).scrollTop(); var scrollHeight = jQuery(document).height() ; var windowHeight = jQuery(this).height(); if (scrollTop + windowHeight >= scrollHeight) { if(not_loading){ refreshMore(); setTimeout(function(){ getMore(); },100); }else{ jQuery(".dl_more").remove(); } } }); function getMore(){ not_loading = false; console.log("getMore......"); jQuery.ajax({ type: 'get', url: "/news/ajax_get_news_recommends/32657", data:{ page:pageNum }, //async: false, dataType: 'json', success: function (resobj) { var totalNum = resobj.total_pages; if(pageNum <= totalNum && resobj.oHtml != '' ){ jQuery(".dl_more").remove(); oList.find('ul').append(resobj.oHtml); refreshMore(); not_loading = true; pageNum++; }else{ not_loading = false; noMore(); } }, error: function (err) { console.log(err); } }); } function noMore(){ jQuery(".dl_more").remove(); if(oList.find(".dl_no_more").length ==0){ oList.append('<div class="dl_no_more" style="font-size:14px; color:#0F8AC6; text-align:center;padding-top:10px; ">我们是很有底线的</div>'); } } function refreshMore(){ if(oList.find(".dl_more").length ==0){ oList.append('<div class="dl_more" style="font-size:14px; color:#0F8AC6; text-align:center;padding-top:10px;">上滑加载更多</div>'); } } }, 200); </script>
dp.SyntaxHighlighter.HighlightAll(‘code’, true, true);
fix_image_size(KaTeX parse error: Expected 'EOF', got '#' at position 6: ('div#̲news_content im…(‘div.comments img’), 620);new WeiboShare({share_buttons: $$(’.share_weibo’)[0], img_scope: $(‘news_content’)});
</div>
5 楼 StormFeng 2018-07-23 11:09
4 楼 StormFeng 2018-07-23 11:03
3 楼 canfengli 2017-09-22 15:39
是啊,现在全转向Spring了,社区蓬勃发展啊
2 楼 snwz 2017-09-14 16:52
1 楼 lixuejian 2017-09-14 15:50