版权声明:转载请注明原始链接 https://blog.csdn.net/sswqzx/article/details/84192251
学习目标:
标准URL映射、ant风格映射、占位符映射、限定请求方法映射、限定请求参数映射、组合注解
1、标准URL映射
语法:@RequestMapping(value=”xxx”)
可定义在Controller类上,也可定义在Controller类中的方法上。
如果Controller类上和方法上都加了@RequestMapping,
请求路径:类上的@RequestMapping的value+方法上的@RequestMapping的value;
如果value不以/开头springmvc会自动加上。
Controller类上的@RequestMapping可以省略,此时请求路径就是:方法上的@RequestMapping的value.
2、ant风格映射
?:通配一个字符 @RequestMapping("sss?/show2")
*:通配0个或多个字符 @RequestMapping("aa*/show3")
**:通配0个或多个路径 @RequestMapping("**/show4")
3、占位符映射
语法:@RequestMapping(value=”show5/{id}/{name}”)
请求路径:http://localhost:8080/hello/show5/1/james
占位符不仅有通配的作用,还可以传递参数。
例如:
@PathVariable(“id”) Long id可以接收id参数;
@PathVariable(“name”) String name可以接收name参数。
@RequestMapping("show5/{id}/{name}")
public ModelAndView test5(@PathVariable("id")Long ids, @PathVariable("name")String names)
注意:@PathVariable(key)中的key值一定要与占位符的名字一致,而形参的名字可以自定义。
4、限定请求方法映射
所谓限定请求方法就是限制请求的方式、如GET HEAD POST PUT PATCH DELETE OPTIONS TRACE
语法:
@RequestMapping(value = "show6",method = RequestMethod.POST)
@RequestMapping(value = "show6",method = RequestMethod.GET)
@RequestMapping(value = "show6",method = RequestMethod.DELETE)
框架源码:
5、限定请求参数映射
语法:@RequestMapping(value=””,params=””)
params=”id” : 请求参数中必须有id
params=”!id” : 请求参数中不能包含id
params=”id=1” : 请求参数中id的值必须为1
params=”id!=1” : 请求参数中id的值必须不能等于1
params={“id”,”name”} : 请求参数中必须包含id和name两个参数
6、组合注解
@GetMapping:相当于@RequestMapping(method=RequestMethod.GET)
@PostMapping:相当于@RequestMapping(method=RequestMethod.POST)
@PutMapping:相当于@RequestMapping(method=RequestMethod.PUT)
@DeleteMapping:相当于@RequestMapping(method=RequestMethod.DELETE)
7、演示:
1、springmvc主入口。配置web.xml 、DispatcherServlet总调度
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- springmvc入口 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 项目启动时,就加载并实例化 -->
<load-on-startup>1</load-on-startup>
<!--当springmvc-servlet.xml没有按照springmvc规定命名就要加载这段来找springmvc.xml-->
<!--<init-param>-->
<!--<param-name>contextConfigLocation</param-name>-->
<!--<param-value>classpath:springmvc.xml</param-value>-->
<!--</init-param>-->
</servlet>
<!--
/*拦截所有请求
/拦截所有请求,不包括jsp
*.do表示拦截所有以.do结尾的请求
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、springmvc配置文件springmvc-servlet.xml 适配器-视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--基于注解、开启注解扫描-->
<context:component-scan base-package="com.day01springmvc.controller"> </context:component-scan>
<!--
把Controller交给spring管理,
在浏览器中通过id属性的值来访问
而我们在web.xml中配置的DispatcherServlet的访问路径是*.do
为了能够让请求进入springmvc框架,所以id的值以do结尾
-->
<!--<bean id="/hello.do" class="com.day01springmvc.controller.HelloController"></bean>-->
<!--
配置视图解析器
prefix:指定视图所在的目录
suffix:指定视图的后缀名
例如:prefix="/WEB-INF/jsp/", suffix=".jsp",当viewname="test"时,
跳转到/WEB-INF/jsp/test.jsp页面
prefix+viewname+suffix 找到WEB-INF/hello.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
3、springmvc业务处理器 HelloController2.java
package com.day01springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
/**
* @ Author :ShaoWei Sun.
* @ Date :Created in 20:58 2018/11/16
*/
@Controller
@RequestMapping("hello")
public class HelloController2 {
/**
* 1、标准url映射
* 语法:@RequestMapping(value = "xxx")
* 处理请求
*/
@RequestMapping("show1")
public ModelAndView test1() {
System.out.println("hello请求进来了............");
ModelAndView mv = new ModelAndView();
//添加一个数据,页面上可以取出这个数据
mv.addObject("msg", "这是第一个springmvc注解程序");
//设置视图的名字
mv.setViewName("hello2");
return mv;
}
/**
* 2.1、ant风格映射
* ? 通配一个字符
* http://localhost:8080/hello/sss9/show2
* @return
*/
@RequestMapping("sss?/show2")
public ModelAndView test2(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ant风格映射:?");
mv.setViewName("hello2");
return mv;
}
/**
* 2.2、ant风格映射
* * 通配0个或多个字符
* http://localhost:8080/hello/aaaabbcc/show3
* @return
*/
@RequestMapping("aa*/show3")
public ModelAndView test3(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ant风格映射: *");
mv.setViewName("hello2");
return mv;
}
/**
* 2.3、ant风格映射
* ** 通配多0个或多个路径
* http://localhost:8080/hello/aa/bb/cc/show4
* @return
*/
@RequestMapping("**/show4")
public ModelAndView test4(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ant风格映射: **");
mv.setViewName("hello2");
return mv;
}
/**
*3、占位符映射
* 语法:@RequestMapping(value=”user/{userId}/{userName}”)
* 请求路径:http://localhost:8080/hello/show5/1/james
* @param ids
* @param names
* @return
*/
@RequestMapping("show5/{id}/{name}")
public ModelAndView test5(@PathVariable("id") Long ids ,@PathVariable("name") String names){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","占位符映射:id:"+ids+";name:"+names);
mv.setViewName("hello2");
return mv;
}
/**
* 4、限定请求方法映射、如限定为Post请求、不是post请求就出错
* http://localhost:8080/hello/show6
* @return
*/
@RequestMapping(value = "show6",method = RequestMethod.POST)
public ModelAndView test6(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","限定请求方法映射");
mv.setViewName("hello2");
return mv;
}
/**
* 5.1、限定请求参数映射
* params=”id” : 请求参数中必须有id
* http://localhost:8080/hello/show7.do?id=9
* @return
*/
@RequestMapping(value = "show7", params = "id")
public ModelAndView test7(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","限定请求参数的映射:必须有id");
mv.setViewName("hello2");
return mv;
}
/**
* 5.2、限定请求参数映射
* params=”!id” : 请求参数中不能包含id
* http://localhost:8080/hello/show8
* @return
*/
@RequestMapping(value = "show8",params = "!id")
public ModelAndView test8(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","限定请求参数的映射:不能有id");
mv.setViewName("hello2");
return mv;
}
/**
* 5.3、限定请求参数映射
* params=”id=1” : 请求参数中id的值必须为1
* http://localhost:8080/hello/show9?id=1
* @return
*/
@RequestMapping(value="show9",params="id=1")
public ModelAndView test9(){
ModelAndView mv = new ModelAndView();
mv.setViewName("hello2");
mv.addObject("msg", "限定请求参数的映射:id=1");
return mv;
}
/**
* 5.4、限定请求参数映射
* params=”id!=1” : 请求参数中id的值必须不能等于1
* http://localhost:8080/hello/show10?id=9
* @return
*/
@RequestMapping(value="show10",params="id!=1")
public ModelAndView test10(){
ModelAndView mv = new ModelAndView();
mv.setViewName("hello2");
mv.addObject("msg", "限定请求参数的映射:id!=1");
return mv;
}
/**
* 5.5、限定请求参数映射
* params={“id”,”name”} : 请求参数中必须包含id和name两个参数
* http://localhost:8080/hello/show11?name=&id=
* @return
*/
@RequestMapping(value="show11",params={"id","name"})
public ModelAndView test11(){
ModelAndView mv = new ModelAndView();
mv.setViewName("hello2");
mv.addObject("msg", "限定请求参数的映射:请求参数中必须有id,name");
return mv;
}
/**
* 6.1、组合注解
* @GetMapping : 相当于@RequestMapping(method=RequestMethod.GET)
* http://localhost:8080/hello/show12
* @return
*/
@GetMapping(value = "show12")
public ModelAndView test12(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","组合注解之@GetMapping");
mv.setViewName("hello2");
return mv;
}
/**
* 6.2、组合注解
* 组合注解之PostMapping
* @PostMapping :相当于@RequestMapping(method=RequestMethod.POST)
* @return
*/
@PostMapping(value="show13")
public ModelAndView test13(){
ModelAndView mv = new ModelAndView();
mv.setViewName("hello2");
mv.addObject("msg", "组合注解之@PostMapping");
return mv;
}
/**
* 6.3、组合注解
* 组合注解之PutMapping
* @PutMapping :相当于@RequestMapping(method=RequestMethod.PUT)
* @return
*/
@PutMapping(value="show14")
public ModelAndView test14(){
ModelAndView mv = new ModelAndView();
mv.setViewName("hello2");
mv.addObject("msg", "组合注解之@PutMapping");
return mv;
}
/**
* 6.4、组合注解
* 组合注解之DeleteMapping
* @DeleteMapping :相当于@RequestMapping(method=RequestMethod.DELETE)
* @return
*/
@DeleteMapping(value="show15")
public ModelAndView test15(){
ModelAndView mv = new ModelAndView();
mv.setViewName("hello2");
mv.addObject("msg", "组合注解之@DeleteMapping");
return mv;
}
}