1.1. @RequestMapping映射请求

SpringMVC 采用 @RequestMapping 注解为控制器指定能够处理那些URL 请求

@requestMapping  能够定义在 类 和 方法 上

package com.ibigsea.springmvc.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloWorld { /**
* 配置@RequestMapping 拦截 localhost:8080/springmvc/hello 请求
* @return
*/
@RequestMapping("/hello")
public String helloWorld() {
System.out.println("hello world");
return "helloworld";
}
}

package com.ibigsea.springmvc.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/hello")
public class HelloWorld { /**
* 配置@RequestMapping 拦截 localhost:8080/springmvc/hello/world 请求
* @return
*/
@RequestMapping("/world")
public String helloWorld(){
System.out.println("hello world");
return "helloworld";
}
}

@RequestMapping

– 类定义处:提供初步的请求映射信息。

相对于 WEB 应用的根文件夹

– 方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若

类定义处未标注 @RequestMapping。则方法处标记的 URL 相对于

WEB 应用的根文件夹

DispatcherServlet 截获请求后,就通过控制器上

@RequestMapping 提供的映射信息确定请求所相应的处理方法。

@RequestMapping 除了能够使用请求 URL 映射请求外。

还能够使用请求方法、请求參数及请求头映射请求

1.2. @RequestMapping限定请求方法、请求參数、请求头

/**
* 接收GET请求
* @return
*/
@RequestMapping(value="/get",method = RequestMethod.GET)
public String get(){
System.out.println("get");
return "get";
} /**
* 接收POST请求
* @return
*/
@RequestMapping(value="/post",method = RequestMethod.POST)
public String post(){
System.out.println("post");
return "post";
} /**
* 仅仅接收 name 參数
* @return
*/
@RequestMapping(value="/params",params="name")
public String params(String name){
System.out.println("hello "+name);
return "helloworld";
} /**
* 仅仅接收请求头中 Content-Type 为 text/html;charset=UTF-8的请求
* @return
*/
@RequestMapping(value="/headers",headers="Content-Type:text/html;charset=UTF-8")
public String headers(){
System.out.println("headers");
return "helloworld";
}

1.3. @RequestMapping匹配符

– ?:匹配文件名称中的一个字符

– *:匹配文件名称中的随意字符

– **:** 匹配多层路径

实例:

URL : /user/*/create

-- /user/bigsea/create 、 /user/sea/create 等URL

URL : /user/**/create

-- /user/big/sea/create 、 /user/sea/big/create 等URL

URL : /user/create??

-- /user/createaa 、/user/createbb

1.4. @PathVariable 注解

带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义

通过 @PathVariable 能够将 URL 中占位符參数绑定到控制器处理方法的入參中:URL 中的 {xxx} 占位符能够通过@PathVariable("xxx") 绑定到操作方法的入參中。

/**
* localhost:8080/springmvc/hello/pathVariable/bigsea
* localhost:8080/springmvc/hello/pathVariable/sea
* 这些URL 都会 运行此方法 而且将 <b>bigsea</b>、<b>sea</b> 作为參数 传递到name字段
* @param name
* @return
*/
@RequestMapping("/pathVariable/{name}")
public String pathVariable(@PathVariable("name")String name){
System.out.println("hello "+name);
return "helloworld";
}

JSP(这里指定全路径):

<h1>pathVariable</h1>
<a href="${pageContext.request.contextPath}/hello/pathVariable/bigsea" > name is bigsea </a>
<br/>
<a href="${pageContext.request.contextPath}/hello/pathVariable/sea" > name is sea</a>
<br/>

执行结果:

hello bigsea
hello sea

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTY3NDc0NTA2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

1.5. @RequestParam 绑定请求參数

在处理方法入參处使用 @RequestParam 能够把请求參数传递给请求方法

– value:參数名

– required:是否必须。

默觉得 true, 表示请求參数中必须包括相应的參数。若不存在。将抛出异常

/**
* 假设 required = true 则表示请求參数相应的 字段 必须存在.假设不存在则会抛出异常<br/>
* @param firstName 能够为null
* @param lastName 不能为null .为null报异常
* @param age age字段表示假设没有 age 參数 则默认值为 0
* @return
*/
@RequestMapping("/requestParam")
public String requestParam(@RequestParam(value="firstName",required=false)String firstName,
@RequestParam( value="lastName" ,required = true) String lastName,
@RequestParam(value="age",required = false ,defaultValue="0")int age) {
System.out.println("hello my name is " + (firstName == null ? "" : firstName)
+ lastName + "," + age +" years old this year");
return "helloworld";
}

Jsp:

<a href="requestParam?firstName=big&lastName=sea" > name is bigsea , age is 0 </a>
<br/>
<a href="requestParam?lastName=sea&age=23" > name is sea , age is 23 </a>
<br/>
<a href="requestParam" > throws exception </a>

执行结果:

hello my name is bigsea,0 years old this year
hello my name is sea,23 years old this year

1.6. @RequestHeader 获取请求头

请求头包括了若干个属性,server可据此获知client的信息。通过 @RequestHeader 就可以将求头中的属性值绑定到处理方法的入參中

	/**
* 获取请求头中的信息
* @RequestHeader 也有 value ,required ,defaultValue 三个參数
* @param userAgent
* @param cookie
* @return
*/
@RequestMapping("/requestHeader")
public String requestHeader(@RequestHeader("User-Agent")String userAgent,@RequestHeader("Cookie")String cookie){
System.out.println("userAgent:["+userAgent+"]");
System.out.println("cookie:["+cookie+"]");
return "helloworld";
}

JSP:

<a href="requestHeader" > requestHeader </a>

执行结果:

userAgent:[Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2383.0 Safari/537.36]
cookie:[JSESSIONID=DA3B15F559349EA2C3F08BE772FCAFD8]

1.7. @CookieValue 获取 cookie值

/**
* 使用@CookieValue 绑定cookie值<br/>
* 注解@CookieValue 也有 value ,required ,defaultValue 三个參数
* @param session
* @return
*/
public String cookieValue(@CookieValue(value = "JSESSIONID", required= false)String session){
System.out.println("JESSIONID:["+session+"]");
return "helloworld";
}

JSP:

<a href="cookieValue" > cookieValue </a>

执行结果

JESSIONID:[A4196EEDFD829B40CC1975F029A61328]

1.8. 源代码分析






watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTY3NDc0NTA2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

版权声明:本文博主原创文章。博客地址:http://blog.csdn.net/a67474506?viewmode=contents

SpringMVC 学习笔记(两) @RequestMapping、@PathVariable和其他注意事项的更多相关文章

  1. SpringMVC 学习笔记(二) @RequestMapping、@PathVariable等注解

    版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/a67474506?viewmode=contents 1.1. @RequestMapping映射请求 Spring ...

  2. SpringMVC:学习笔记(2)——RequestMapping及请求映射

    SpringMVC--RequestMapping及请求映射 @RequestMapping 说明 Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请 ...

  3. 史上最全的SpringMVC学习笔记

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  4. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

  5. SpringMVC学习笔记之二(SpringMVC高级参数绑定)

    一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...

  6. springmvc学习笔记(19)-RESTful支持

    springmvc学习笔记(19)-RESTful支持 标签: springmvc springmvc学习笔记19-RESTful支持 概念 REST的样例 controller REST方法的前端控 ...

  7. SpringMVC:学习笔记(5)——数据绑定及表单标签

    SpringMVC——数据绑定及表单标签 理解数据绑定 为什么要使用数据绑定 基于HTTP特性,所有的用户输入的请求参数类型都是String,比如下面表单: 按照我们以往所学,如果要获取请求的所有参数 ...

  8. SpringMVC:学习笔记(8)——文件上传

    SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...

  9. springmvc学习笔记(简介及使用)

    springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...

随机推荐

  1. 从零開始学习OpenCL开发(一)架构

    多谢大家关注 转载本文请注明:http://blog.csdn.net/leonwei/article/details/8880012 本文将作为我<从零開始做OpenCL开发>系列文章的 ...

  2. 树上点对统计poj1741(树的点分治)

    给定一棵树,边上有权值,要统计有多少对点路径的权值和<=k 分治算法在树的路径中的应用 这个论文里面有分析. 任意两点的路径,要么过根结点,要么在子树中.如果在子树中,那么只要递归处理就行了. ...

  3. POJ 2533-Longest Ordered Subsequence(DP)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34454   Acc ...

  4. Oracle SQL Lesson (3) - 使用单行函数自定义输出

    大小写转换函数LOWER('SQL Course') = sql courseUPPER('SQL Course') = SQL COURSEINITCAP('SQL Course') = Sql C ...

  5. C++ - Identifier not found

     This is because forward declaration in C++: Compiler needs to know function prototype when functi ...

  6. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  7. Nagios监控生产环境redis群集服务战

    前言:     曾经做了cacti上展示redis性能报表图.能够看到redis的性能变化趋势图,可是还缺了实时报警通知的功能,如今补上这一环节. 在redis服务瓶颈或者异常时候即使报警通知,方便d ...

  8. oracle在schema是什么意思?

    看来有些人还在schema不明白的真正含义,今天,我再次整理.我希望能帮助. 我们先来看看它们的定义:A schema is a collection of database objects (use ...

  9. 求pi 的公式

    pi = 3.1415926..... 下面用c 语言来求解PI 现有公式 (pi*pi)/6 = 1 + 1/(2*2) + 1/(3*3) + ... + 1/(n*n); #include &l ...

  10. Ios 该图显示其出现的相关问题定义UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:&#39;

    解决这个问题 在 加上个 标示符 Cell 自己定义 customCell .h 代码例如以下 ViewController.m 文件里 代码例如以下 执行结果 吕 图坚持直接在这里 不行