转发地址: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. pycharm 2018 激活码

    本页面破解不止一种,选择适合你的使用 --------------------------------------------------------------------------------- ...

  2. tar归档压缩命令和zip归档 和7zip压缩命令;库文件归档ar命令

    第一.tar 归档 tar -c 创建归档文件包 tar -x 释放归档文件包 tar -t 查看归档文件包 tar -v 显示归档包操作过程信息 tar -f 指定归档文件名 案例1:归档 /hom ...

  3. 51 Nod 1430 奇偶游戏(博弈)

    1430 奇偶游戏 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 有n个城市,第i个城市有ai个人.Daenery ...

  4. 洛谷 P1966 火柴排队 题解

    归并排序 很玄学的一道题目,用另类的方法求出逆序对的数量就可以AC 我的思路是这样的: 按照题目,输入数据用两个数组a,b储存, 同时,用另外两个数组c,d分别对应前面两个a,b储存, 就是前面两个的 ...

  5. P1853 投资的最大效益

    题目背景 约翰先生获得了一大笔遗产,他暂时还用不上这一笔钱,他决定进行投资以获得更大的效益.银行工作人员向他提供了多种债券,每一种债券都能在固定的投资后,提供稳定的年利息.当然,每一种债券的投资额是不 ...

  6. 19、Executor原理剖析与源码分析

    一.原理图解 二.源码分析 1.Executor注册机制 worker中为Application启动的executor,实际上是启动了这个CoarseGrainedExecutorBackend进程: ...

  7. 《挑战30天C++入门极限》引言

    作为一个长篇的C++入门教程,无论如何也应该有这么个引言,可是文笔并不好的我,想了很久也不知道该如何写...... 仔细想想,与其把这篇短文当作教程的引言,其实它更应该是一篇引导初学者步入C++殿堂的 ...

  8. 如何手动安装WordPress主题和插件(Linux)

    1. 去官网找到你想使用的插件或主题,并复制下载地址 2. cd到WordPress网站目录下的插件或主题目录下 这里以我的为例: 安装插件:cd /var/www/html/wp-content/p ...

  9. 页面截取字段和转码,页面截取字段时候需要进入JS

    截取字段    ${fn:substring(info.cpflmc,0,20)}${fn:length(info.cpflmc)>40?'...':''}             表头list ...

  10. nginx代理mysql

    实验环境: 两台编译安装的mysql                            一台编译安装的nginx 192.168.3.1                               ...