什么是Spring MVC?
Spring MVC 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一。
Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架
Spring Web MVC处理请求的流程
Spring Web MVC架构
导入jar包
我们基于Spring mvc框架进行开发,需要依赖一下的spring jar包:
在WEB-INF目录下新建lib文件夹,并将上面的jar包放入其中。
配置文件web.xml(WEB-INF下)
pom.xml文件如下↓↓↓↓
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.neuedu</groupId>
<artifactId>springMVC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springMVC Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<springFramework.version>5.1.5.RELEASE</springFramework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springFramework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springFramework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springFramework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springFramework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springFramework.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
<build>
<finalName>springMVC</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 配置DispatchcerServlet前端控制器,请求集合点 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注意: <param-value>classpath:springmvc.xml</param-value>用于配置spring mvc的配置文件的位置和名称,这里说明会新建一个springmvc.xml的配置文件。
这里的servlet-mapping表示拦截的模式,这里是“/”,表示对于非jsp请求进行拦截。
Springmvc.xml(scr下)
在src目录下新建springmvc.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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.neusoft.controller"></context:component-scan>
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/views/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean>
</beans>
<context:component-scan base-package="com.neusoft.controller"></context:component-scan>
表示spring监听的范围,这里是在com.neusoft.controller下
HelloWorldController.java(com.neusoft.controller下)
@Controller层原servlet层
package com.neuedu.web;
import com.neuedu.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Controller
//@RestController是可以直接返回json的注解
@RequestMapping("user")
public class HelloController {
// 第一种方式,注解直接接post和get,在注解的括号里面写接的请求方式
@RequestMapping(value = "add",method = RequestMethod.GET)
public void hello1(){
System.out.println("hello springmvc add");
}
// 注解直接可以该成getmapping就是接get请求
@GetMapping("del")
public void hello2(){
System.out.println("hello springmvc del");
}
// 注解直接可以该成postmapping就是接post请求
// 并且接请求的方式为在方法的括号中写requestparam(接的名字)类型 形参
@PostMapping("select")
public void hello3(@RequestParam("username") String name){
System.out.println("hello springmvc select"+name);
}
// 接的是post请求,传过来的直接封装到student里,前提是student的属性和前台传过来的名字一样
@PostMapping("select2")
public void hello3(Student student){
System.out.println("hello springmvc select"+student);
}
// 接的是session的时候可以直接在方法的括号里面写httpsession 就可以获取session
@PostMapping("select3")
public void hello4(Student student,HttpSession session){
System.out.println("hello springmvc select"+student);
}
// 下面讲返回值
/**
* 返回值类型
* void -- 响应ajax请求,response.getwriter().print()
* String --可以转发和重定向
* ModelAndView -- 只能做转发
*
*/
@GetMapping("select4")
public String hello5(Student student, Model model){
// 如果姓名是mike,跳转到success.jsp
if(student.getUsername().equals("mike")){
// return "/success.jsp";
// 下面这个是往request里放数据
model.addAttribute("username","mike");
return "forward:/success.jsp";//forward也可以不写,默认的就是forward
}else{
// 否则跳转失败页面
return "redirect:/fail.jsp";
}
}
public ModelAndView hello6(Student student, Model model){
ModelAndView modelAndView = new ModelAndView();
// 如果姓名是mike,跳转到success.jsp
if(student.getUsername().equals("mike")){
// return "/success.jsp";
// 下面这个是往request里放数据
modelAndView.addObject("userinfo","mike000");
modelAndView.setViewName("success");
}else{
// 否则跳转失败页面
modelAndView.setViewName("fail");
}
return modelAndView;
}
@GetMapping("serch")
public void serch(@RequestParam("id") Integer id, HttpServletResponse response){
}
@GetMapping("serch")
// @ResponseBody
public Student serch1(@RequestParam("id") Integer id, HttpServletResponse response){
Student student = new Student();
return student;
}
}