什么是springmvc
Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。
项目中加入springmvc支持
导入依赖
c
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<slf4j.version>1.7.7</slf4j.version>
<jcl-over-slf4j.version>1.7.7</jcl-over-slf4j.version>
<log4j2.version>2.9.1</log4j2.version>
<log4j2.disruptor.version>3.2.0</log4j2.disruptor.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!--日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.7.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.2</version>
</dependency>
</dependencies>
springMVC配置文件
将课件中的spring-mvc.xml放入WEB-INF目录下,详细信息见配置文件中的注释。
注:关于注解扫描的优化:
在使用spring+springmvc时会存在两个上下文,一个父上下文spring上下文,一个子上下文springmvc上下文,springmvc上下文可以访问父上下文(即spring上下文),但spring上下文不能访问到子上下文(即springmvc上下文)。
web.xml配置
将随课件提供的web.xml放入WEB-INF目录,替换原来的配置文件。需要理解配置文件的各个部分的作用。
中文编码处理
将随课件提供的EncodingFiter.java,将入项目并在web.xml中配置,以便于处理中文编码。
写一个简单的Controller
c
package com.zking.mvcdemo.controller;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.jws.WebParam;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(String name, Model model){
model.addAttribute("name",name);
return "hello";
}
@RequestMapping("/welcome")
public String welcome(String name,Model model){
model.addAttribute("name",name);
return "redirect:/hello";
}
}
注: 示例中用到的注解:
@Controller:该注解在springmvc中用于标记一个控制器,类似的注解还有@RestController,不同的是@RestController专门用于标记一个rest风格的接口,相当于@ResponseBody + @Controller合在一起的作用。
@RequestMapping:该用于标记请求路径,在有些参考资料中也称之为服务端点。该注释可以用在方法上也可以用在类上。
SpringMVC处理请求的流程
主要组件
1 首先用户发送请求-->DispatherServlet
2 DispatcherServlet-->HandlerMapping
3 DispatcherServlet-->HandlerAdapter
4 HandlerAdapter-->处理器功能处理方法的调用
5 ModelAndView的逻辑视图名-->ViewRecolver
6 View-->渲染
7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束