2.1 @RequestMapping

  • @RequestMapping是SpringMVC的核心注解,负责访问的url与调用方法之间的映射;
  • @RequestMapping可以放在类和方法上;
    • @RequestMapping的属性produces属性控制response返回的形式;
    • @RequestMapping的属性method属性控制接受访问的类型,不写不做限制,本例为演示方便全部都是get请求;
  • @ResponseBody(放在方法上或者返回值类型前)将方法参数放置在web body的body中(返回的不是页面而是你所控制的字符)
  • @RequestBody(放在方法参数前)将方法参数放置在web request的body中(如提交一个json对象作为参数-在03点睛Spring MVC 4.1-REST演示)
  • produces的内容是指定返回的媒体类型让浏览器识别
    • 如返回text/plain的话,chrome浏览器下network显示Response的Content-Type:text/plain;
    • 如返回application/json的话,chrome浏览器下network显示Response的application/json;
    • 因本节无页面,在03点睛Spring MVC 4.1-REST有只管的阐述和演示;
  • 这节使用@RequestMapping演示常用映射场景

2.2 演示

  • 传值对象
package com.wisely.web;

public class DemoObj {

private Long id;

private String name;
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #795da3;" class="pl-en">DemoObj</span>() {
<span style="color: #ed6a43;" class="pl-v">super</span>();
} <span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #795da3;" class="pl-en">DemoObj</span>(<span style="color: #333333;" class="pl-smi">Long</span> <span style="color: #ed6a43;" class="pl-v">id</span>, <span style="color: #333333;" class="pl-smi">String</span> <span style="color: #ed6a43;" class="pl-v">name</span>) {
<span style="color: #ed6a43;" class="pl-v">super</span>();
<span style="color: #ed6a43;" class="pl-v">this</span><span style="color: #a71d5d;" class="pl-k">.</span>id <span style="color: #a71d5d;" class="pl-k">=</span> id;
<span style="color: #ed6a43;" class="pl-v">this</span><span style="color: #a71d5d;" class="pl-k">.</span>name <span style="color: #a71d5d;" class="pl-k">=</span> name;
}
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #333333;" class="pl-smi">Long</span> <span style="color: #795da3;" class="pl-en">getId</span>() {
<span style="color: #a71d5d;" class="pl-k">return</span> id;
}
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #a71d5d;" class="pl-k">void</span> <span style="color: #795da3;" class="pl-en">setId</span>(<span style="color: #333333;" class="pl-smi">Long</span> <span style="color: #ed6a43;" class="pl-v">id</span>) {
<span style="color: #ed6a43;" class="pl-v">this</span><span style="color: #a71d5d;" class="pl-k">.</span>id <span style="color: #a71d5d;" class="pl-k">=</span> id;
}
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #333333;" class="pl-smi">String</span> <span style="color: #795da3;" class="pl-en">getName</span>() {
<span style="color: #a71d5d;" class="pl-k">return</span> name;
}
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #a71d5d;" class="pl-k">void</span> <span style="color: #795da3;" class="pl-en">setName</span>(<span style="color: #333333;" class="pl-smi">String</span> <span style="color: #ed6a43;" class="pl-v">name</span>) {
<span style="color: #ed6a43;" class="pl-v">this</span><span style="color: #a71d5d;" class="pl-k">.</span>name <span style="color: #a71d5d;" class="pl-k">=</span> name;
}

}

  • 控制器 TestController
package com.wisely.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody; @Controller //声明为控制器bean

@RequestMapping("/test")// 根地址为http://localhost:8080/testSpringMVC/test

public class TestController {

//response媒体类型(MediaType)为text/plain,编码是utf-8

@RequestMapping(produces = "text/plain;charset=UTF-8")

//映射地址为http://localhost:8080/testSpringMVC/test

@ResponseBody //此注解让返回值不是页面,也是将结果字符串直接返回

public String root(HttpServletRequest request){

return "url:"+request.getRequestURL()+" 可以访问此方法";

}
<span style="color: #a71d5d;" class="pl-k">@RequestMapping</span>(<span style="color: #0086b3;" class="pl-c1">value</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>/add<span class="pl-pds">"</span></span>,<span style="color: #0086b3;" class="pl-c1">produces</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>text/plain;charset=UTF-8<span class="pl-pds">"</span></span>)
<span style="color: #969896;" class="pl-c">//映射地址为http://localhost:8080/testSpringMVC/test/add</span>
<span style="color: #a71d5d;" class="pl-k">@ResponseBody</span>
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #333333;" class="pl-smi">String</span> <span style="color: #795da3;" class="pl-en">add</span>(<span style="color: #333333;" class="pl-smi">HttpServletRequest</span> <span style="color: #ed6a43;" class="pl-v">request</span>){
<span style="color: #a71d5d;" class="pl-k">return</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>url:<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>request<span style="color: #a71d5d;" class="pl-k">.</span>getRequestURL()<span style="color: #a71d5d;" class="pl-k">+</span><span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span> 可以访问此方法<span class="pl-pds">"</span></span>;
} <span style="color: #a71d5d;" class="pl-k">@RequestMapping</span>(<span style="color: #0086b3;" class="pl-c1">value</span> <span style="color: #a71d5d;" class="pl-k">=</span> {<span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>/remove<span class="pl-pds">"</span></span>,<span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>/delete<span class="pl-pds">"</span></span>},<span style="color: #0086b3;" class="pl-c1">produces</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>text/plain;charset=UTF-8<span class="pl-pds">"</span></span>)
<span style="color: #969896;" class="pl-c">//映射地址为http://.../test/remove(或http://.../test/delete)</span>
<span style="color: #a71d5d;" class="pl-k">@ResponseBody</span>
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #333333;" class="pl-smi">String</span> <span style="color: #795da3;" class="pl-en">remove</span>(<span style="color: #333333;" class="pl-smi">HttpServletRequest</span> <span style="color: #ed6a43;" class="pl-v">request</span>){
<span style="color: #a71d5d;" class="pl-k">return</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>url:<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>request<span style="color: #a71d5d;" class="pl-k">.</span>getRequestURL()<span style="color: #a71d5d;" class="pl-k">+</span><span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span> 可以访问此方法<span class="pl-pds">"</span></span>;
} <span style="color: #969896;" class="pl-c">//获取request参数</span>
<span style="color: #969896;" class="pl-c">//获取路径参数</span>
<span style="color: #a71d5d;" class="pl-k">@RequestMapping</span>(<span style="color: #0086b3;" class="pl-c1">value</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>/get<span class="pl-pds">"</span></span>,<span style="color: #0086b3;" class="pl-c1">produces</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>text/plain;charset=UTF-8<span class="pl-pds">"</span></span>)
<span style="color: #969896;" class="pl-c">//映射路径http://.../test/get?id=123</span>
<span style="color: #a71d5d;" class="pl-k">@ResponseBody</span>
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #333333;" class="pl-smi">String</span> <span style="color: #795da3;" class="pl-en">passRequestParam</span>(<span style="color: #a71d5d;" class="pl-k">@RequestParam</span> <span style="color: #333333;" class="pl-smi">Long</span> <span style="color: #ed6a43;" class="pl-v">id</span>,<span style="color: #333333;" class="pl-smi">HttpServletRequest</span> <span style="color: #ed6a43;" class="pl-v">request</span>){
<span style="color: #333333;" class="pl-smi">System</span><span style="color: #a71d5d;" class="pl-k">.</span>out<span style="color: #a71d5d;" class="pl-k">.</span>println(<span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>id为<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>id);
<span style="color: #a71d5d;" class="pl-k">return</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>url:<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>request<span style="color: #a71d5d;" class="pl-k">.</span>getRequestURL()<span style="color: #a71d5d;" class="pl-k">+</span><span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span> 可以访问此方法<span class="pl-pds">"</span></span>; } <span style="color: #969896;" class="pl-c">//获取路径参数</span>
<span style="color: #a71d5d;" class="pl-k">@RequestMapping</span>(<span style="color: #0086b3;" class="pl-c1">value</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>/{id}<span class="pl-pds">"</span></span>,<span style="color: #0086b3;" class="pl-c1">produces</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>text/plain;charset=UTF-8<span class="pl-pds">"</span></span>)
<span style="color: #969896;" class="pl-c">//映射路径http://.../test/123</span>
<span style="color: #a71d5d;" class="pl-k">@ResponseBody</span>
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #333333;" class="pl-smi">String</span> <span style="color: #795da3;" class="pl-en">passPathVariable</span>(<span style="color: #a71d5d;" class="pl-k">@PathVariable</span> <span style="color: #333333;" class="pl-smi">Long</span> <span style="color: #ed6a43;" class="pl-v">id</span>,<span style="color: #333333;" class="pl-smi">HttpServletRequest</span> <span style="color: #ed6a43;" class="pl-v">request</span>){
<span style="color: #333333;" class="pl-smi">System</span><span style="color: #a71d5d;" class="pl-k">.</span>out<span style="color: #a71d5d;" class="pl-k">.</span>println(<span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>id为<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>id);
<span style="color: #a71d5d;" class="pl-k">return</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>url:<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>request<span style="color: #a71d5d;" class="pl-k">.</span>getRequestURL()<span style="color: #a71d5d;" class="pl-k">+</span><span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span> 可以访问此方法<span class="pl-pds">"</span></span>; } <span style="color: #969896;" class="pl-c">//获得对象</span>
<span style="color: #a71d5d;" class="pl-k">@RequestMapping</span>(<span style="color: #0086b3;" class="pl-c1">value</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>/pass<span class="pl-pds">"</span></span>,<span style="color: #0086b3;" class="pl-c1">produces</span> <span style="color: #a71d5d;" class="pl-k">=</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>text/plain;charset=UTF-8<span class="pl-pds">"</span></span>)
<span style="color: #969896;" class="pl-c">//映射路径http://.../test/pass?id=123&amp;name=wyf</span>
<span style="color: #a71d5d;" class="pl-k">@ResponseBody</span>
<span style="color: #a71d5d;" class="pl-k">public</span> <span style="color: #333333;" class="pl-smi">String</span> <span style="color: #795da3;" class="pl-en">passObj</span>(<span style="color: #333333;" class="pl-smi">DemoObj</span> <span style="color: #ed6a43;" class="pl-v">obj</span>,<span style="color: #333333;" class="pl-smi">HttpServletRequest</span> <span style="color: #ed6a43;" class="pl-v">request</span>){
<span style="color: #333333;" class="pl-smi">System</span><span style="color: #a71d5d;" class="pl-k">.</span>out<span style="color: #a71d5d;" class="pl-k">.</span>println(<span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>对象的id和名称分别为为:<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>obj<span style="color: #a71d5d;" class="pl-k">.</span>getId()<span style="color: #a71d5d;" class="pl-k">+</span><span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>/<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>obj<span style="color: #a71d5d;" class="pl-k">.</span>getName());
<span style="color: #a71d5d;" class="pl-k">return</span> <span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span>url:<span class="pl-pds">"</span></span><span style="color: #a71d5d;" class="pl-k">+</span>request<span style="color: #a71d5d;" class="pl-k">.</span>getRequestURL()<span style="color: #a71d5d;" class="pl-k">+</span><span style="color: #183691;" class="pl-s"><span class="pl-pds">"</span> 可以访问此方法<span class="pl-pds">"</span></span>; }

}

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

Spring MVC--@RequestMapping的更多相关文章

  1. Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable--转载

    原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.b ...

  2. Spring MVC @RequestMapping注解详解

    @RequestMapping 参数说明 value:定义处理方法的请求的 URL 地址.(重点) method:定义处理方法的 http method 类型,如 GET.POST 等.(重点) pa ...

  3. Spring MVC — @RequestMapping原理讲解-1

    转载地址 :http://blog.csdn.net/j080624/article/details/56278461 为了降低文章篇幅,使得文章更目标化,简洁化,我们就不例举各种@RequestMa ...

  4. Spring MVC @RequestMapping注解详解(2)

    @RequestMapping 参数说明 value:定义处理方法的请求的 URL 地址.(重点) method:定义处理方法的 http method 类型,如 GET.POST 等.(重点) pa ...

  5. spring mvc requestmapping 配置多个

    参考 import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation. ...

  6. Spring MVC @RequestMapping浅析

    简介:@RequestMappingRequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.RequestMapp ...

  7. spring mvc @RequestMapping method 不写的话,默认GET、POST都支持,根据前端方式自动适应

    @RequestMapping(value="/") method 不写的话,默认GET.POST都支持,根据前端方式自动适应.

  8. SpringBoot系列: Spring MVC视图方法的补充

    SpringMVC 视图方法的参数, 已经在这个文章中写得非常清楚了, 链接为 https://www.cnblogs.com/morethink/p/8028664.html 这篇文章做一些补充. ...

  9. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  10. Spring MVC基本配置@controller和@RequestMapping解释

    一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml   dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...

随机推荐

  1. HDU 3232 &amp;&amp; UVA 12230 (简单期望)

    Crossing Rivers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  3. [NPM] Publish npm packages using npm publish

    In this lesson we will publish our package. We will first add a prepublish script that runs our buil ...

  4. C/C++函数指针声明

    前天看APUE,看到signal的声明竟然是 void (*signal(int,void(*)(int)))(int); 初看下面,还真是看不出这是啥意思.道行太浅,仅仅能看到这样的函数指针 voi ...

  5. WebStorm(Amaze开发工具)--JavaScript 开发工具

    WebStorm(Amaze开发工具)--JavaScript 开发工具 一.总结 1.webstorm:前段开发神器,应该比sublime好用. 2.webstorm功能:支持显示图片宽高,标签重构 ...

  6. 【编程】辨异 —— proxy 与 delegate

    二者分别对应着设计模式中的代理模式和委托模式. proxy:译为代理, 被代理方(B)与代理方(A)的接口完全一致. 主要使用场景(语义)应该是:为简化编程(或无法操作B),不直接把请求交给被代理方( ...

  7. golang encoding/json

    package main import ( "bytes" "encoding/json" "fmt" ) type ColorGroup ...

  8. h5背景

    1.背景属性复习: background-image background-color background-repeat background-position background-attachm ...

  9. Mongodb总结1-启动和Shell脚本

    2013年,还在秒针,当时听说了Mongodb,就学习了下,搞了下HelloWorld.主要是熟悉Mongodb的启动.命令行的Shell脚本.Java访问的CRUD. 今天,由于需要,再次回顾和进一 ...

  10. c#中反射的用法(即如何根据字符找到已定义的变量)

    2013-07-20 08:06 720人阅读 评论(0) 收藏 举报  分类: C#(9)  作者同类文章 X 版权声明:本文为博主原创文章,未经博主允许不得转载. 常常羡慕javascript中, ...