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 ...
随机推荐
- asp.net+ tinymce粘贴word
公司做的项目需要用到粘贴Word功能.就是将word内容一键粘贴到网页编辑器(在线富文本编辑器)中.Chrome+IE默认支持粘贴剪切板中的图片,但是我要粘贴的文章存在word里面,图片多达数十张,我 ...
- stos指令
mov ecx,30mov eax,0cccccccchrep stos dword prt es:[edi]stos指令,它的功能是将eax中的数据放入的edi所指的地址中,同时,edi会增加4个字 ...
- C# 字符串String相关
是否可以继承String类 不能,因为从定义上看String类是sealed类[密封]故不可以继承.当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承.若硬要写,则编译不通过 字符 ...
- ssm + ehcache 运行异常
异常: DEBUG [net.sf.ehcache.CacheManager@295af581] - Checking for update...DEBUG [net.sf.ehcache.Cache ...
- vue日常学习(2)
1.组件学习之内容分发 1.1 作用域插槽 父级 <div class="parent"> <child> <template scope=" ...
- 如何快速关联/修改Git远程仓库地址
如何快速关联/修改Git远程仓库地址?按照如下步骤即可快速实现关联/修改Git远程仓库地址: 删除本地仓库当前关联的无效远程地址,再为本地仓库添加新的远程仓库地址 git remote -v //查看 ...
- Git的使用(6) —— 自动填写远端Git用户名密码
1. 问题描述 公司项目组用私服的Git远程版本库,每一次推送和拉取服务都需要输入用户名和密码,过于繁琐. 2. 解决方法 Windows系统提供了"管理Windows凭据"的功能 ...
- SPM(Software Project Management)课程感想
今天要说的是软件项目管理课程学习后的一些心得体会.这学期我选修了软件项目管理课程,进行了共8周的学习. 其实,进入大三后,我们开设了各种专业选修课,通过对各种课程的学习,我见识到了丰富多样的知识体 ...
- Linux静态路由配置
配置了多块网卡后,需要指定数据包由哪块网卡发送,否则可能无法访问内网,这就要用到静态路由了. 配置静态路由有多种方式: 1.修改 /etc/rc.local 文件,这样每次重启后就会自动添加,如: e ...
- CheatEngine查看PE header
先打开进程,炉石传说. 然后选择MemoryViewer 在MemoryViewer界面,Tools菜单,然后选择Dissect PE headers 然后查看mono.dll的信息 0x357A0是 ...