@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. mybatis在xml文件中处理转义字符

    第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. AND start_date <= CURRENT_DATE AND end_date >= CURRENT_DA ...

  2. C++ 编译期封装-Pimpl技术

    Pimpl技术——编译期封装 Pimpl 意思为“具体实现的指针”(Pointer to Implementation), 它通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏, 是隐藏实 ...

  3. 数据库艰难求生之路(基础:创建数据库表格)part1

     创建表格 1.创建表格之identity create table TableName( id ,), col_1 ), col_2 ) ) 这是一个简单的表格创建,identity这个是指在创建表 ...

  4. .net MVC +EF+VUE做回合制游戏(二)

    Emmm,游戏中的属性购买页面 话不多说先上代码 <form id="vue" action="/ltgdGame.Web/Main/Index" met ...

  5. vue v-for动画bug

    因为是v-for 循环 出来的,:key = "index" 会出现问题,所以,需要把:key="XXX"换成其他属性就好了. 链接参考: https://se ...

  6. Dynamics 365-关于Solution的那些事(一)

    关于CRM Solution,我准备写两到三篇的博客来做下介绍:包括一些基本信息,超大solution,还有增量更新solution操作等. CRM中的component,都是放在一个名叫Soluti ...

  7. arcgis api for js入门开发系列十九图层在线编辑

    本篇主要讲述的是利用arcgis api实现图层在线编辑功能模块,效果图如下: 实现思路: 1.arcgis server发布的FeatureServer服务提供的图层在线编辑能力: 2.实现的在线编 ...

  8. (七) Keras 绘制网络结构和cpu,gpu切换

    视频学习来源 https://www.bilibili.com/video/av40787141?from=search&seid=17003307842787199553 笔记 首先安装py ...

  9. 数据结构学习java(一点五)链式顺序表(链表)

    java中没有将指针暴露给用户(以前做过看过一篇文章写有java中是有指针的,只是被藏起来了),所以得使用引用的方式. 何为引用请看下面这篇文章(写的很不错,当然肯定比我写的好): https://w ...

  10. HTTP中GET和POST的区别主要是那些,面试中可以加分的该说那些?

    面试回答: GET请求在URL中传送的参数是有长度限制的,而POST没有. GET比POST更不安全,因为参数直接暴露在URL上,所以不能用来传递敏感信息. GET参数通过URL传递,POST放在Re ...