完整的项目案例: 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>
转发结果: