SpringMVC总结一:快速入门的基础上简单介绍一下请求映射的方式:

1,标准映射规则

  1、 @RequestMapping可以设置在类上,也可以设置在方法上

  2、 请求的映射规则是:类上的RequestMapping + 方法上的RequestMapping

  3、 如果没有写 / ,SpringMVC会自动补全

  4、 类上的RequestMapping可以省略,这时直接用方法的RequestMapping访问

  5、 路径不可重复

//加上Controller注解
@Controller
@RequestMapping("/test")//添加映射路径,可用来设置url
public class HelloController {
//映射注解
@RequestMapping("/helloSpringMVC")
public ModelAndView helloSpringMVC() {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");//设置视图名字
mv.addObject("msg", "hello springmvc 注解版 ===" + new Date().toLocaleString());
return mv;
}
}

2,Ant风格映射

  1、? 匹配一个字符,如/hello? 可以匹配/hello1  /hello2,但是不能匹配/hello或者/hello12

  2、* 匹配0个或多个字符, 如/hello/*  可以匹配/hello/demo或者/hello/demo2

  3、**匹配0个或多个路径,如/hello/**  可以匹配/hello路径下的所有子路径,比如/hello/dd或者/hello/dd/cc/aa

  4、最长匹配优先:

    现在有4中映射路径:1,/hello/demo  2,/hello/**/abc  3,/hello/**  4,/**

    如果请求为/hello/demo 那么优先匹配1,但是请求/hello/ddd则会匹配3,请求/hello/222/abc则匹配2,请求/ddd匹配4

//加上Controller注解
@Controller
@RequestMapping("/test")//添加映射路径,可用来设置url
public class HelloController {
//ant风格映射
@RequestMapping("/**/testAntMapping")
public ModelAndView antMapping() {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");//设置视图名字
mv.addObject("msg", "antMapping test ===" + new Date().toLocaleString());
return mv;
}
}

3,占位符映射

  例如商品的url路径一般都是.../product/xxxxx.html,这个xxxxx可以设置成占位符

  @RequestMapping(value="/product/{productid}") :占位符为productid,访问的路径可以是/product/123456或者/product/222等

  通过@PathVariable 可以提取 URI 模板模式中的{xxx}中的xxx变量。

//加上Controller注解
@Controller
@RequestMapping("/test")//添加映射路径,可用来设置url
public class HelloController {
//占位符映射
@RequestMapping("/pathvariable/{id}")
public ModelAndView pathVariableMapping(@PathVariable("id") int id1) {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");//设置视图名字
mv.addObject("msg", "pathvariable id=" + id1);
return mv;
}
}

SpringMVC总结二:Controller的请求映射方式(RequestMapping)简单介绍的更多相关文章

  1. HTTP请求方式中8种请求方法(简单介绍)

    简单介绍 HTTP是超文本传输协议,其定义了客户端与服务器端之间文本传输的规范.HTTP默认使用80端口,这个端口指的是服务端的端口,而客户端使用的端口是动态分配的.当我们没有指定端口访问时,浏览器会 ...

  2. salesforce 零基础学习(二十九)Record Types简单介绍

    在项目中我们可能会遇见这种情况,不同的Profile拥有不同的页面,页面中的PickList标签可能显示不同的值.这个时候,使用Record Types可以很便捷的搞定需求. Record Types ...

  3. SpringMVC(十二) RequestMapping使用POJO作为参数

    将一个普通的JAVA类对象作为一个参数传入. POJO类Address: package com.tiekui.springmvc.pojo; public class Address { priva ...

  4. SpringMVC(十七-二十) ModelAttribute 注解

    有点难理解. 修饰方法是表示在该控制器的所有目标方法执行前都执行该modelattribute注解的方法. 修饰参数是表示什么?修饰参数时@modelattributes(value="xx ...

  5. SpringMVC中的@Controller和@RequestMapping到底什么鬼?

    1.1 @Controller是什么 首先看个例子: @Controller @RequestMapping("/blog") public class BlogControlle ...

  6. SpringMVC总结三:请求Controller返回视图类型以及请求方式、参数介绍

    视图解析,请求Controller返回的视图类型: @Controller @RequestMapping("/test") public class TestController ...

  7. springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误。

    springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误. 1.场景 如果在web.xml中servlet-mapping的url-pattern设置的是 ...

  8. SpringMVC学习笔记一(请求流程和配置,启动项目)

    springmvc请求流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. 3.处理器映 ...

  9. springmvc 发送PUT 和 DELETE 请求

    一: 发送 DELETE 或者 PUT 请求: 1.在表单中加入一个隐藏的参数: _method  , 值是 DELETE (或者PUT) <form action="springmv ...

随机推荐

  1. android 进制转换方法

    import android.util.Log; public class CommandHelper { public static String intToHexString(int value) ...

  2. 利用Github免费搭建个人主页(个人博客)

    之前闲着, 利用Github搭了个免费的个人主页. 涉及: Github注册 Github搭建博客 域名选购 绑定域名 更多 一  Github注册 在地址栏输入地址:http://github.co ...

  3. LinuxCentos6安装MySql workbench

    参考地址:http://www.cnblogs.com/elaron/archive/2013/03/19/2968699.html

  4. matlab算法转为c语言注意事项

    matlab算法转为c语言后,影响c语言效率的关键在于multiword的产生,基于此会有multiword加减法和乘除法,极大消耗资源,减少甚至消除multiword很重要,需注意的是:算法中尽量减 ...

  5. throw、try 和 catch

    try 语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块. JavaScript 语句 try 和 catch 是成对出现的. ...

  6. Log4net系列一:Log4net搭建之文本格式输出【转】

    前言 项目开发中,记录项目日志是必须的,如果非要说日志的重要性(日志可看做,飞机的黑匣子,或者汽车的行车记录仪),根据等级进行记录,方便我们排查相关问题,以后项目运维中,也方便很多.基本上我们进入一家 ...

  7. 使用 key 登录时分开记录操作历史记录

    线上服务器一般都是配置 key 登录,一个账号可以多个工作人员连接,操作命令历史却全部记录在一个文件中,当然后查看某条命令是谁执行的时候就不好查了.这时候我们就可以通过配置 histroy 相关环境变 ...

  8. 使用 ip 进行系统网络配置

    检查是否有安装iproute工具 rpm -qa | grep iproute 查看所有可用接口 ip link show 启用或禁用接口 ip link set down eth1 ip link ...

  9. php MySQL使用rand函数随机取记录(转)

    php MySQL使用rand函数随机取记录 如何在mysql中使用随机数, 如何写一个语句能一下更新几百条MYSQL数据! 需要测试MYSQL数据库,里面有一个上万条数据的数据库,如何写一个PHP文 ...

  10. 字符串(二)(PHP)

    1.大段文本在PHP中应该如果表示? 答: <?php $str = <<<aaa hello word; fjasdflj fjslad aaa;date_sub() aaa ...