Spring MVC 请求映射 (二)
完整的项目案例: springmvc.zip
目录

实例
项目结构:

一、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!-- 配置请求总控器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
二、配置dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启用注解并扫描 -->
<context:component-scan base-package="edu.nf.ch02.controller"/>
<!-- 启用mvc注解驱动-->
<mvc:annotation-driven/> <mvc:default-servlet-handler/> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>
三、Controller类:
package edu.nf.ch02.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; /**
* @author wangl
* @date 2018/10/29
*/
@Controller
/**
* @RequestMapping也可以标注在类上,
* 通常用来指定请求的命名空间
*/
@RequestMapping("/user")
public class RequestMapController { /**
* method属性用于指定能支持的请求方法,它的值为一个数组
* @return
*/
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public ModelAndView getUser(){
System.out.println("getUser...");
return new ModelAndView("index");
} @RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(){
System.out.println("login...");
return new ModelAndView("index");
} @RequestMapping(value = "/reg", method = {RequestMethod.POST,RequestMethod.GET})
public ModelAndView reg(){
System.out.println("reg...");
return new ModelAndView("index");
} /**
* 在spring4.0之后,加入了明确的方法请求处理的注解
* @return
*/
@GetMapping("/login2")
public ModelAndView getUser2(){
System.out.println("getUser2...");
return new ModelAndView("index");
} @PostMapping("/reg2")
public ModelAndView reg2(){
System.out.println("login2...");
return new ModelAndView("index");
}
}
html网页请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="get" action="user/login">
<input type="submit" value="login"/>
</form>
</body>
</html>
转发结果:

Spring MVC 请求映射 (二)的更多相关文章
- 2017.3.31 spring mvc教程(二)核心流程及配置详解
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- Spring MVC的映射请求
一.SpringMVC常用注解 @Controller 声明Action组件 @Service 声明Service组件 @Service("myMovieLister" ...
- spring mvc 请求转发和重定向(转)
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
- spring mvc 请求转发和重定向
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
- 手写Spring MVC框架(二) 实现访问拦截功能
前言 在上一篇文章中,我们手写了一个简单的mvc框架,今天我们要实现的功能点是:在Spring MVC框架基础上实现访问拦截功能. 先梳理一下需要实现的功能点: 搭建好Spring MVC基本框架: ...
- Spring MVC入门(二)—— URI Builder模式
URI Builder Spring MVC作为一个web层框架,避免不了处理URI.URL等和HTTP协议相关的元素,因此它提供了非常好用.功能强大的URI Builder模式来完成,这就是本文重点 ...
- Spring MVC请求流程
Spring MVC 发起请求到前端控制器DispathServlet 前端控制器请求处理器映射器 handerMapping查找handler 处理器映射器handerMapping像前端控制器返回 ...
- spring mvc请求过程
spring mvc处理请求过程 1. 首先客户端发送一个HTTP请求,Web服务器接收这个请求,如果匹配DispatcherServlet的请求映射路径,web容器将请求转交给Dispatch ...
随机推荐
- 02 Tensorflow Hello World程序
打开python idle,输入: import tensorflow as tf # 创建常量 hello = tf.constant('Hello,world!') # 创建会话 sess = t ...
- ES6躬行记(11)——对象
在第5篇中,讲解了多个对象字面量的改进,本节将重点介绍两个新增的静态方法,以及对象属性的重复处理和枚举顺序. 一.Object.is() 此方法用于判断两个值是否相同,内部实现了SameValue算法 ...
- [转]innodb的锁时间
本文转自:https://www.cnblogs.com/sunss/p/3170132.html 观察innodb的锁时间,需要关注: mysqladmin extended-status -r - ...
- [心得]如何快速利用SqlMap做安全檢測
SqlMap的介紹我就不詳提啦!SqlMap是用來尋找與做Sql-injection的一款工具 在使用前你需要安裝python唷!安裝的過程我們就不介紹了. 直接來看如何快速使用SqlMap吧! 這裡 ...
- MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射
在上一章中我们学习了<MyBatis学习总结(一)——ORM概要与MyBatis快速起步>,这一章主要是介绍MyBatis核心配置文件.使用接口+XML实现完整数据访问.输入参数映射与输出 ...
- C# Redis安装 使用教程
前言:lz自打工作以来第一次遇到电脑问题需要重装系统,全盘格式化.打击是沉痛的.特别伤. 然后需要重新在本地部署 redis.这是写这篇博客的原因.希望对大家有所帮助,安装资源和引用DLL可以引用 ...
- .Net C# 使用Redis
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...
- [android] 保存文件到手机内存
/*****************2016年5月4日 更新*******************************/ 知乎:Android 没有沙盒保护机制吗,WhatsApp 信息为何可被随 ...
- mysql使用存储过程&函数实现批量插入
写这边文章的目的,是想结合mysql 存储过程+函数完成一个批量删除的功能吧...正好也好加深下对procedure和function的熟练操作吧...废话不多说,我就直接上表结构啦哈,如下: cre ...
- [leetcode](4.21)3. 最长重复子串
给定字符串 S,找出最长重复子串的长度.如果不存在重复子串就返回 0. 示例 1: 输入:"abcd" 输出:0 解释:没有重复子串. 示例 2: 输入:"abbaba& ...