@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. AssetsUtils【读取assets、res/raw、./data/data/包名/目录下的文件】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装了以下功能: 1.读取assets目录下的资源html.文件.图片,将文件复制到SD卡目录中: 2.读取res/raw目录下的文 ...

  2. Solr 15 - Solr添加和更新索引的过程 (文档的路由细节)

    目录 1 添加文档的细节 1.1 注册观察者 - watcher 1.2 文档的路由 - document route 1.2.1 路由算法 1.2.2 Solr路由的实现类 1.2.3 implic ...

  3. 安装Phalcon报错:gcc: Internal error: Killed (program cc1)

    起因 安装Phalcon可以参考github上面的README.md 下面是我在阿里云ECS服务器上面执行命令的过程: # 安装依赖 sudo yum install php-devel pcre-d ...

  4. Flink-Kafka-Connector Flink结合Kafka实战

    戳更多文章: 1-Flink入门 2-本地环境搭建&构建第一个Flink应用 3-DataSet API 4-DataSteam API 5-集群部署 6-分布式缓存 7-重启策略 8-Fli ...

  5. Linux文本三剑客超详细教程---grep、sed、awk

    awk.grep.sed是linux操作文本的三大利器,合称文本三剑客,也是必须掌握的linux命令之一.三者的功能都是处理文本,但侧重点各不相同,其中属awk功能最强大,但也最复杂.grep更适合单 ...

  6. (转载)JSON.stringfy()和JSON.parse()的作用

    原文链接:https://www.cnblogs.com/shytong/p/4960418.html 一篇详细的介绍和对比,转载自 博客园 “很好玩的博客” 的一片博文,非常感谢他贡献优质文章.

  7. Office组件无法正常使用的解决方法

    问题与现象     开发时调用Office组件,代码编译是通过的,但在运行时当ApplicationClass对象初始化后程序出现异常.     异常信息如下:     无法将类型为“Microsof ...

  8. 5分钟掌握var,let和const异同

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者.原文出处:https://dzone.com/articles/javascript-difference-b ...

  9. mysql7笔记----存储过程实例

    mysql创建存储过程 DROP PROCEDURE IF EXISTS getCreateTimes /*前面要写DELIMITER $$ 或DELIMITER // */ DELIMITER $$ ...

  10. mssql sqlserver 分组排序函数row_number、rank、dense_rank用法简介及说明

    在实际的项目开发中,我们经常使用分组函数,对组内数据进行群组后,然后进行组内排序:如:1:取出一个客户一段时间内,最大订单数的行记录2: 取出一个客户一段时间内,最后一次销售记录的行记录——————— ...