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. 高中生活-第9篇-开学之初的“失足”囧事,"刻舟求剑"导致腿折了

    时间过得好快啊,上次发表"高中生活-第8篇:夏天的空调,冬天的味道"是2014年9月30日,一转眼,就是一年啊. 我自己以为,很多人可能都以为,我又半途而废了,实则不是哦~ 行百里 ...

  2. SSO单点登录学习总结(2)——基于Cookie+fliter单点登录实例

    1.使用Cookie解决单点登录 技术点: 1.设置Cookie的路径为setPath("/").即Tomcat的目录下都有效 2.设置Cookie的域setDomain(&quo ...

  3. arcgis webapp builder 安装试用

    ArcGIS WebApp Builder 是针对开发者的,用于高速构建基于HTML5/Javascript 技术的美观的 Web应用的一个工具. 用过Flex版本号的AppBuilder应该非常清楚 ...

  4. 10. ZooKeeper之搭建伪集群模式。

    转自:https://blog.csdn.net/en_joker/article/details/78673456 在集群和单机两种模式下,我们基本完成了分别针对生产环境和开发环境ZooKeeper ...

  5. vb.net structure 定义静态数组

    Const RAS95_MaxEntryName = 256 Const RAS95_MaxDeviceName = 128 Const RAS_MaxDeviceType = 16 Structur ...

  6. 【CodeForces】Gargari and Bishops

    依据贪心能够知道,放置的教主必须不能相互攻击到(也就是不在一条对角线上)才干够使得结果最大化. 依据观察能够得到教主相互不攻击的条件是他的坐标和互为奇偶(x + y) 之后直接暴力,处理每一个坐标对角 ...

  7. [转]C#连接操作mysql实例

    本文转自:http://hi.baidu.com/zhqngweng/item/c4d2520cb7216877bfe97edf 第三方组件:Mysql.Data.dll说明:去官方网站下载Mysql ...

  8. js里的表格数组某个key去重

    如Elemgnt的table绑定的数据要某个key是唯一的 var myarry = [ {name: 'liuyang',age :13}, {name:'jike',age:15}, {name: ...

  9. Centos 6 vnc 部署

    一.安装gnome桌面环境 yum groupinstall -y 'X Window System' yum groupinstall -y "Desktop" 二.部署vnc ...

  10. Docker使用Dockerfile创建Centos(tomcat+jdk)镜像

    原文链接:https://blog.csdn.net/qq_37936542/article/details/80824389 Docker构建镜像的方法主要有两种:    (1)使用docker c ...