02点睛Spring MVC 4.1-@RequestMapping
转发地址: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有只管的阐述和演示;
- 如返回text/plain的话,chrome浏览器下network显示Response的
- 这节使用@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的更多相关文章
- spring mvc 注解@Controller @RequestMapping @Resource的详细例子
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring MVC 之@Controller@RequestMapping详解
一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...
- 01点睛Spring MVC 4.1-搭建环境
转发:https://www.iteye.com/blog/wiselyman-2213906 1.1 简单示例 通篇使用java config @Controller声明bean是一个控制器 @Re ...
- 03点睛Spring MVC 4.1-REST
转发:https://www.iteye.com/blog/wiselyman-2214290 3.1 REST REST:Representational State Transfer; REST是 ...
- 06点睛Spring MVC 4.1-文件上传
6.1 文件上传 在控制器参数使用@RequestParam("file") MultipartFile file接受单个文件上传; 在控制器参数使用@RequestParam(& ...
- 05点睛Spring MVC 4.1-服务器端推送
转发:https://www.iteye.com/blog/wiselyman-2214626 5.1 服务器端推送 SSE(server send event)是一种服务器端向浏览器推送消息的技术, ...
- 04点睛Spring MVC 4.1-拦截器
转发地址:https://www.iteye.com/blog/wiselyman-2214292 4.1 拦截器 拦截器实现了对每一个请求处理之前和之后进行相关的处理,类似于Servlet的filt ...
- 08点睛Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)
8.1 配置 Spring MVC的配置是通过继承WebMvcConfigurerAdapter类并重载其方法实现的; 前几个教程已做了得配置包括 01点睛Spring MVC 4.1-搭建环境 配置 ...
- Spring mvc中@RequestMapping 6个基本用法
Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: Java代码 @Reques ...
随机推荐
- sqoop job 实现自动增量导入
一.测试环境 1.MySQL表结构 mysql> show create table autoextend\GCREATE TABLE `autoextend` ( `id` bigint(2 ...
- 利用fgetc合并2个源文件的内容,到一个新的文件中
#include <stdio.h> #include <stdlib.h> //功能: 合并2个源文件的内容,到一个新的文件中 int main(int a,char *ar ...
- 洛谷 P1004 方格取数 题解
P1004 方格取数 题目描述 设有 \(N \times N\) 的方格图 \((N \le 9)\),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字\(0\).如下图所示(见样例): ...
- 洛谷 P5436 【XR-2】缘分 题解
P5436 [XR-2]缘分 题目背景 世间万物都置身于缘分编织的大网中.缘分未到,虽历经千劫,却不能相遇.缘分到了,在草原上都能等到一艘船.--<一禅小和尚> 题目描述 一禅希望知道他和 ...
- 留言板welcome here friends!
欢迎留言!!! 另附本人信息栏 \(cnblogs\): ShineEternal \(洛谷\):vercont \(CSDN\) \(blog\): ShineEternal \(github\) ...
- redis系列(四):切换RDB备份到AOF备份
1.准备环境 redis.conf服务端配置如下: daemonize yes port logfile /data//redis.log dir /data/ dbfilename dbmp.rdb ...
- python2 && python3 的 input函数
Python2.x中的input()函数input()函数让我们明确我们输入的是数字格式还是字符格式,就是我们自己要知道我们想要的是什么,数字格式直接输入,字符格式必须加上单引号或者双引号,以确定我们 ...
- PHP无法使用curl_init()函数
主要针对在Ubuntu16.04搭建CRMEB环境时,监测环境会出现一个curl_init问题,这时只需执行如下命令即可解决: sudo apt-get install php-curl
- ubuntu之路——day8.3 RMSprop
RMSprop: 全称为root mean square prop,提及这个算法就不得不提及上篇博文中的momentum算法 首先来看看momentum动量梯度下降法的过程: 在RMSprop中: C ...
- Windows 开始 运行中所打开的默认程序以及优先级
Windows 开始 运行中所打开的默认程序以及优先级 Default app/softwares and priority for Windows/start/run 商务合作,科技咨询,版权转让: ...