@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. 【activity任务栈】浅析

    背景知识 Activity四种启动模式 标准模式 standard 栈顶复用模式 singleTop 栈内复用模式 singleTask 单例模式 singleInstance 任务栈 Activit ...

  2. Docker Swarm搭建多服务器下Docker集群

    对于有多台服务器来讲,如果每一台都去手动操控,那将会是一件非常浪费时间的事情,毕竟时间这东西,于我们而言,十分宝贵,或许在开始搭建环境的时候耗费点时间,感觉是正常的,我也如此,花费大堆时间在采坑和填坑 ...

  3. [开发技巧]·Numpy中对axis的理解与应用

    [开发技巧]·Numpy中对axis的理解与应用 1.问题描述 在使用Numpy时我们经常要对Array进行操作,如果需要针对Array的某一个纬度进行操作时,就会用到axis参数. 一般的教程都是针 ...

  4. Asp.Net Core 轻松学-一行代码搞定文件上传

    前言     在 Web 应用程序开发过程中,总是无法避免涉及到文件上传,这次我们来聊一聊怎么去实现一个简单方便可复用文件上传功能:通过创建自定义绑定模型来实现文件上传. 1. 实现自定义绑定模型 1 ...

  5. 1.4部署到IIS「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 很多人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运行Asp.N ...

  6. 基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)

    今天我们来盘一盘Socket通讯和WebSocket协议在即时通讯的小应用——聊天. 理论大家估计都知道得差不多了,小编也通过查阅各种资料对理论知识进行了充电,发现好多demo似懂非懂,拷贝回来又运行 ...

  7. KVO讲解

    最近一直在写swift项目,没有时间更新自己的技术博客,以前在博客里面写过KVO的底层原理,今天我们来看一下KVO的整个使用过程和使用场景(附有demo),大约花大家10-15分钟时间,希望大家看完博 ...

  8. 杭电ACM2018--母牛的故事

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. C# T4 模板 数据库实体类生成模板(带注释,娱乐用)

     说明:..,有些工具生成实体类没注释,不能和SqlServer的MS_Description属性一起使用,然后照着网上的资源,随便写了个生成模板,自娱自乐向,其实卵用都没有参考教程    1.htt ...

  10. Identity4实现服务端+api资源控制+客户端请求

    准备写一些关于Identity4相关的东西,最近也比较对这方面感兴趣.所有做个开篇笔记记录一下,以便督促自己下一个技术方案方向 已经写好的入门级别Identity4的服务+api资源访问控制和简单的客 ...