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. vue踩坑记-在项目中安装依赖模块npm install报错

    在维护别人的项目的时候,在项目文件夹中安装npm install模块的时候,报错如下: npm ERR! path D:\ShopApp\node_modules\fsevents\node_modu ...

  2. javascript脚本从载入浏览器到显示执行的过程解析

    版权声明:本文为博主原创文章,未经博主允许不得转载. 简单的代码: <script type="text/javascript" src="xxx.js" ...

  3. 一个虐你千百遍的问题:“RPC好,还是RESTful好?”

    看到知乎上有这样一个问题 WEB开发中,使用JSON-RPC好,还是RESTful API好? 还有其他优秀的推荐方案吗? -------------------------------------- ...

  4. [CortexM0--stm32f0308]discovery开发板

        问题描写叙述:stm32提供了很多IC入门级开发板,价格还是蛮廉价的. stm32f0308-discovery就是一款cortex-m0架构的入门级开发板. 例如以下对其进行下简介. IO便 ...

  5. arguments对象----不定参数的实现方式

    function format(string) { var args = arguments; var pattern = new RegExp("%([1-" + argumen ...

  6. ios 推断是qq,银行卡,手机号等等公用的方法。

    #import <Foundation/Foundation.h> typedef enum  {     IdentifierTypeKnown = 0,     IdentifierT ...

  7. amazeui页面分析之登录页面

    amazeui页面分析之登录页面 一.总结 1.tpl命名空间:tpl命名空间的样式都是从app.css里面来的,app.css用用来移动网站开发的样式 2.表单样式:am-form到am-form- ...

  8. Lucene 查询方式

    QueryParser类 QueryParser类对输入字符串的解析 格    式 含    义 “David” 在默认的字段中检索“David”关键字 “content:David” 在“conte ...

  9. (转) 设置sqlplus中的退格键

    转自:http://blog.itpub.net/26110315/viewspace-717249/ 有些时候当你使用sqlplus登录到数据库中的时候,敲错了命令想要删除修改的时候,发现以前敲入的 ...

  10. 让自己的软件实现拖拽打开文件(覆盖WM_DROPFILES,使用DragQueryFile,DragFinish API函数)

    作者: 帅宏军 //声明 protected    procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES; --------- ...