@RequestMapping

这个注解标注在方法名上,如

 /**
* 拦截所有请求:
* @RequestMapping(value="/*", method = {RequestMethod.GET})
* http://localhost:8080/SpringMVCTag/helloworld/xd
*/
//@AuthPassport
@RequestMapping(value="/*", method = {RequestMethod.GET})
public ModelAndView urlTest(){
ModelAndView modelAndView = new ModelAndView();
//跳转到 urltest.jsp或者其他后缀的页面,这个要看springmvc的配置文件里是怎么设置的,
modelAndView.setViewName("urltest");
return modelAndView;
}

@RequestMapping(value="/*")

1 就是拦截所有请求

结合上面的一小段代码就是拦截:

类似于http://localhost:8080/SpringMVCTag/helloworld/我是任意字符,

http://localhost:8080/SpringMVCTag/helloworld/comaosdfjoa

这样的请求。

2 拦截多个(多个)请求

例如:

 /**
* 拦截多种请求
* 如: @RequestMapping(value={"/index","/hello"})
* 访问URL:http://localhost:8080/SpringMVCTag/helloworld/index
* @return
* @throws SQLException
*/
//@AuthPassport
@RequestMapping(value={"/index","/hello"})
public ModelAndView index() throws SQLException{ //throw new SQLException("数据库异常。"); ModelAndView modelAndView = new ModelAndView();
//在jsp中可以通过 ${message} 的形式来获取绑定的值
modelAndView.addObject("message", "Hello World!");
modelAndView.setViewName("index");
return modelAndView;
}

3 @RequestMapping 注解结合 @PathVariable注解可以在控制器(也就是具体的方法)的入参里

获取到URL中的占位参数

例如:

 /**
* @RequestMapping 结合 @PathVariable 注解可以获取URL中带占位的那个参数值,
* 具体到这个例子中,在方法名中Integer id这个入参的值就是URL中的值,
* 假如URL为:
* http://localhost:8080/SpringMVCTag/helloworld/detail/yangdandan
* 那么在跳转到的detail.jsp中通过
* ${cxrr}的形式就可以获取到 yangdandan 这个字符串
* @param id
* @return
*/
@RequestMapping(value="/detail/{thepara}", method = {RequestMethod.GET})
public ModelAndView getDetail(@PathVariable(value="thepara") String thepara){ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("crxx", thepara);
modelAndView.setViewName("detail");
return modelAndView;
}

明天继续填坑。

项目所在位置如下图:

BaseController.java:

 package com.demo.web.controllers;

 import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
*
* @author Wei
* @time 2017年4月20日 下午3:56:51
*/
public abstract class BaseController { @ExceptionHandler
public String exception(HttpServletRequest request, Exception e) { request.setAttribute("exceptionMessage", e.getMessage()); // 根据不同的异常类型进行不同处理
if(e instanceof SQLException)
return "testerror";
else
return "error";
} }
HelloWorldController.java
 package com.demo.web.controllers;

 import java.sql.SQLException;

 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.RequestMethod;
import org.springframework.web.servlet.ModelAndView; /**
*
* @author Wei
* @time 2017年4月20日 下午4:56:43
*/
@Controller
@RequestMapping(value = "/helloworld")
public class HelloWorldController extends BaseController {
/**
* 拦截所有请求:
* @RequestMapping(value="/*", method = {RequestMethod.GET})
* http://localhost:8080/SpringMVCTag/helloworld/xd
*/
//@AuthPassport
@RequestMapping(value="/*", method = {RequestMethod.GET})
public ModelAndView urlTest(){
ModelAndView modelAndView = new ModelAndView();
//跳转到 urltest.jsp或者其他后缀的页面,这个要看springmvc的配置文件里是怎么设置的,
modelAndView.setViewName("urltest");
return modelAndView;
}
/**
* 拦截多种请求
* 如: @RequestMapping(value={"/index","/hello"})
* 访问URL:http://localhost:8080/SpringMVCTag/helloworld/index
* @return
* @throws SQLException
*/
//@AuthPassport
@RequestMapping(value={"/index","/hello"})
public ModelAndView index() throws SQLException{ //throw new SQLException("数据库异常。"); ModelAndView modelAndView = new ModelAndView();
//在jsp中可以通过 ${message} 的形式来获取绑定的值
modelAndView.addObject("message", "Hello World!");
modelAndView.setViewName("index");
return modelAndView;
}
/**
* @RequestMapping 结合 @PathVariable 注解可以获取URL中带占位的那个参数值,
* 具体到这个例子中,在方法名中Integer id这个入参的值就是URL中的值,
* 假如URL为:
* http://localhost:8080/SpringMVCTag/helloworld/detail/yangdandan
* 那么在跳转到的detail.jsp中通过
* ${cxrr}的形式就可以获取到 yangdandan 这个字符串
* @param id
* @return
*/
@RequestMapping(value="/detail/{thepara}", method = {RequestMethod.GET})
public ModelAndView getDetail(@PathVariable(value="thepara") String thepara){ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("crxx", thepara);
modelAndView.setViewName("detail");
return modelAndView;
} @RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", method = {RequestMethod.GET})
public ModelAndView regUrlTest(@PathVariable(value="name") String name, @PathVariable(value="age") Integer age){ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name", name);
modelAndView.addObject("age", age);
modelAndView.setViewName("regurltest");
return modelAndView;
} @RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET})
public ModelAndView paramsTest(){ ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("paramstest");
return modelAndView;
} }

SpringMVC的各种注解的更多相关文章

  1. SpringMVC自动扫描@Controller注解的bean

    若要对@Controller注解标注的bean进行自动扫描,必须将<context:component-scan base-package="包路径.controller"/ ...

  2. 关于spring-mvc的InitBinder注解的参数

    关于spring-mvc的InitBinder注解的参数 通过Spring-mvc的@InitBinder注释的方法可以对WebDataBinder做一些初始化操作.比如设置Validator. 我一 ...

  3. SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题

    先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文件中使用<mvc:annotation-driven />去自动注册DefaultAnnota ...

  4. SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...

  5. [转载]SpringBoot系列: SpringMVC 参数绑定注解解析

    本文转载自 https://www.cnblogs.com/morethink/p/8028664.html, 作者写得非常好, 致谢! SpringMVC 参数绑定注解解析   本文介绍了用于参数绑 ...

  6. 使用SpringMVC的@CrossOrigin注解解决跨域请求问题

    跨域问题,通俗说就是用ajax请求其他站点的接口,浏览器默认是不允许的.同源策略(Same-orgin policy)限制了一个源(orgin)中加载脚本或脚本与来自其他源(orgin)中资源的交互方 ...

  7. spring mvc 返回乱码SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题

    原文地址:https://www.cnblogs.com/fzj16888/p/5923232.html 先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文 ...

  8. (三)SpringMVC之常用注解

    SpringMVC的常用注解 注解 说明 @Controller 用于说明这个类是一个控制器 @RequestMapping 用于注释一个控制器类或者控制器类的方法 @RequestParam 用于将 ...

  9. springMVC+springJDBC+Msql注解模式

    最近基于Spring4.X以上的版本写了一个springMVC+springJDBC+Msql注解模式的一个项目,之中也遇到过很多问题 ,为了防止以后遇到同样问题现记录一下知识点以及详细配置. 首先我 ...

  10. SpringMVC使用@Valid注解进行数据验证

    SpringMVC使用@Valid注解进行数据验证   from:https://blog.csdn.net/zknxx/article/details/52426771 我们在做Form表单提交的时 ...

随机推荐

  1. 从零开始学习PYTHON3讲义(十六)(连载完)学习资源包下载

    <从零开始PYTHON3>学习资源包下载 课程连载已经完全结束. 经过整理校对,这里把在课程中出现过的源码和练习答案示例源码全部打包提供下载: https://pan.baidu.com/ ...

  2. async/await使用深入详解

    async和await作为异步模型代码编写的语法糖已经提供了一段时间不过一直没怎么用,由于最近需要在BeetleX webapi中集成对Task方法的支持,所以对async和await有了深入的了解和 ...

  3. Git认证方式https和ssh的原理及比较

    常见的代码托管平台GitHub.GitLab和BitBucket等,基本都会使用Git作为版本控制工具.平台一般都提供两种认证方式https和ssh.了解该过程能够更加自由的配置和使用,本文就来简单聊 ...

  4. 服务器配置java

    先去链接下载jdk or jre(服务器上这个就好) 然后解压 tar 下载的文件,放到/usr/local/java/jdk_xxx下面 -v: 可视化显示进度. Enables verbose m ...

  5. Spring Cloud Alibaba基础教程:Sentinel使用Nacos存储规则

    通过上一篇<使用Sentinel实现接口限流>的介绍,相信大家对Sentinel已经有了初步的认识.在Spring Cloud Alibaba的整合封装之下,接口限流这件事情可以非常轻易的 ...

  6. Javascript中双等号(==)隐性转换机制

    在Javascript中判断相等关系有双等号(==)和三等号(===)两种.其中双等号(==)是值相等,而三等号(===)是严格相等(值及类型是否完全相等). 因此有几个常识知识: 1.对于strin ...

  7. (摘)C#生成随机数的三种方法

    随机数的定义为:产生的所有数字毫无关系. 在实际应用中很多地方会用到随机数,比如需要生成唯一的订单号. 在C#中获取随机数有三种方法: 一.Random 类 Random类默认的无参构造函数可以根据当 ...

  8. C#的一些获取时间的例子

    从周一到周日的顺序,获取排序数值: int i = DateTime.Now.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6; 获取某日起,星期一日期 ...

  9. SpringBoot2 application.properties方式加载配置文件

    application.properties jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:33 ...

  10. python3爬取网页图片路径并写入文件

    import reimport urllib.request # 获取网页文件def getHtml(url): response = urllib.request.urlopen('https:// ...