转发地址:https://www.iteye.com/blog/wiselyman-2213907

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; public DemoObj() {
super();
} public DemoObj(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = 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()+" 可以访问此方法";
} @RequestMapping(value = "/add",produces = "text/plain;charset=UTF-8")
//映射地址为http://localhost:8080/testSpringMVC/test/add
@ResponseBody
public String add(HttpServletRequest request){
return "url:"+request.getRequestURL()+" 可以访问此方法";
} @RequestMapping(value = {"/remove","/delete"},produces = "text/plain;charset=UTF-8")
//映射地址为http://.../test/remove(或http://.../test/delete)
@ResponseBody
public String remove(HttpServletRequest request){
return "url:"+request.getRequestURL()+" 可以访问此方法";
} //获取request参数
//获取路径参数
@RequestMapping(value = "/get",produces = "text/plain;charset=UTF-8")
//映射路径http://.../test/get?id=123
@ResponseBody
public String passRequestParam(@RequestParam Long id,HttpServletRequest request){
System.out.println("id为"+id);
return "url:"+request.getRequestURL()+" 可以访问此方法"; } //获取路径参数
@RequestMapping(value = "/{id}",produces = "text/plain;charset=UTF-8")
//映射路径http://.../test/123
@ResponseBody
public String passPathVariable(@PathVariable Long id,HttpServletRequest request){
System.out.println("id为"+id);
return "url:"+request.getRequestURL()+" 可以访问此方法"; } //获得对象
@RequestMapping(value = "/pass",produces = "text/plain;charset=UTF-8")
//映射路径http://.../test/pass?id=123&name=wyf
@ResponseBody
public String passObj(DemoObj obj,HttpServletRequest request){
System.out.println("对象的id和名称分别为为:"+obj.getId()+"/"+obj.getName());
return "url:"+request.getRequestURL()+" 可以访问此方法"; } }

02点睛Spring MVC 4.1-@RequestMapping的更多相关文章

  1. spring mvc 注解@Controller @RequestMapping @Resource的详细例子

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  2. Spring MVC 之@Controller@RequestMapping详解

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

  3. 01点睛Spring MVC 4.1-搭建环境

    转发:https://www.iteye.com/blog/wiselyman-2213906 1.1 简单示例 通篇使用java config @Controller声明bean是一个控制器 @Re ...

  4. 03点睛Spring MVC 4.1-REST

    转发:https://www.iteye.com/blog/wiselyman-2214290 3.1 REST REST:Representational State Transfer; REST是 ...

  5. 06点睛Spring MVC 4.1-文件上传

    6.1 文件上传 在控制器参数使用@RequestParam("file") MultipartFile file接受单个文件上传; 在控制器参数使用@RequestParam(& ...

  6. 05点睛Spring MVC 4.1-服务器端推送

    转发:https://www.iteye.com/blog/wiselyman-2214626 5.1 服务器端推送 SSE(server send event)是一种服务器端向浏览器推送消息的技术, ...

  7. 04点睛Spring MVC 4.1-拦截器

    转发地址:https://www.iteye.com/blog/wiselyman-2214292 4.1 拦截器 拦截器实现了对每一个请求处理之前和之后进行相关的处理,类似于Servlet的filt ...

  8. 08点睛Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)

    8.1 配置 Spring MVC的配置是通过继承WebMvcConfigurerAdapter类并重载其方法实现的; 前几个教程已做了得配置包括 01点睛Spring MVC 4.1-搭建环境 配置 ...

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

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

随机推荐

  1. 常用的两种web单点登录SSO的实现原理

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...

  2. Apache Kylin在美团点评的应用

      本文原载自大数据杂谈微信公众号. 感谢美团点评工程师高大月撰文并授权转载. 高大月,美团点评工程师,Apache Kylin PMC成员,目前主要在美团点评数据平台负责OLAP查询引擎的建设. 背 ...

  3. bzoj 5408: string 后缀自动机 + LCT

    联赛前练练码力. code: #include <vector> #include <cstdio> #include <cstring> #include < ...

  4. WebAPI学习

    WebAPI概述 今天的web计算平台包含了广泛的功能,其中的大部分均可以通过API(应用程序编程接口)访问. web平台归为6个基本设施,都会用到webapi,包括存储服务.消息服务.计算服务.信息 ...

  5. Linux中三种SCSI target的介绍之LIO

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/scaleqiao/article/deta ...

  6. 记一次vue+vuex+vue-router+axios+elementUI开发(一)

    记录自己的vue开发之旅,方便之后查询 一.开发环境 1.安装node.js  自带npm https://nodejs.org/en/ 2. 全局安装vue-cli脚手架 npm install v ...

  7. <英狼> 团队作业2--王者光耀

    队员 陶俊宇_031702113 卞永亨_031702229 唐怡_031702109 Github 吉哈---King-Shines

  8. Android智能手机上的音频浅析【转】

    本文转载自:https://blog.csdn.net/david_tym/article/details/80903385 手机可以说是现在人日常生活中最离不开的电子设备了.它自诞生以来,从模拟的发 ...

  9. sass - &的作用

    6.8. & in SassScript 就像在选择器中使用时一样,&在SassScript中引用当前父选择器.它是一个逗号分隔的列表和空格分隔的列表.例如: .foo.bar .ba ...

  10. Flutter -------- 新手 WanAndroid 项目练习

    一个简单Flutter项目wanandroid,先前用Kotlin来开发过,适合新手练习. 用到的库 包含功能: http+数据解析 网络请求数据列表展示 Banner轮播 WebView跳转详情 D ...