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 ...
随机推荐
- leetcode — trapping-rain-water
/** * Source : https://oj.leetcode.com/problems/trapping-rain-water/ * * Created by lverpeng on 2017 ...
- 【原创】深入理解c++的右值引用
0 左值和右值 一个左值表达式代表的是对象本身,而右值表达式代表的是对象的值:变量也是左值. 1 右值引用作用 为了支持移动操作(包括移动构造函数和移动赋值函数),C++才引入了一种新的引 ...
- 绝对路径的表示方式为什么是"/usr"而不是"//usr"
今天闲逛贴吧,竟然看到有个人问绝对路径的表示方式为什么不是//usr/local而是/usr/local.原文: 我想99%的人都没想过这个问题,都理所当然的认为:它不就是根"/" ...
- shell编程基础(六): 透彻解析查找命令find
find 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只要你具有相应的权限. ...
- WPF 自定义Command
无参Command: internal class DelegateCommand : ICommand { private readonly Action _execute; private rea ...
- Ubuntu 18.1远程登录服务器--ssh的安装
默认的Ubuntu 18.1桌面版没有安装ssh远程登录服务: 打开"终端窗口",输入"sudo apt-get update"-->回车-->&q ...
- C#开源框架(转载)
Json.NET http://json.codeplex.com/ Json.Net 是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Li ...
- Linux-学习patch命令打补丁,diff命令制作补丁(3)
patch:通过补丁文件,来对原文件打补丁 diff: 比较两个文件,然后生成一个补丁文件 1.patch用法 patch -p[剥离层级] <[补丁文件] 2.patch命令 ...
- 使用Mybatis Generator插件自动生成映射文件(cmd无法进入文件,dns服务器对区域没有权威等问题)遇到问题
使用Mybatis Genertor插件自动生MyBatis所需要的DAO接口,实体模型类,Mapping映射文件,将生成的代码赋值到项目工程中即可. 有命令行,Eclipse插 ...
- java多线程高并发
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81293321 “高并发和多线程”总是被一起提 ...