一,简介
1.1,什么是MVC
Spring MVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,通过把(M)Model,(V)View,(C)Controller分离,将web层进行职责解耦,把复杂的web应用分成逻辑清晰的几部分,简化开发,减少出错,方便组内开发人员之间的配合。
1.2,SpringMVC的优缺点
MVC 优点:
多视图共享一个模型,大大提高代码的可重用性
MVC三个模块相互独立,松耦合架构
控制器提高了应用程序的灵活性和可配置性
有利于软件工程化管理
完美的系统架构 = 松耦合+高重用性+高扩展性
MVC 缺点:
原理复杂
增加了系统结构和实现的复杂性
视图对模型数据的低效率访问
1.3,MVC模式
1.视图(View)-对应组件:JSP或者HTML文件
2.控制器(Controller)-对应组件:Servlet
3.模型(Model)-对应组件:JavaBean
JSP Model1
JSP Model2
4.Servlet:接受前端请求并调用JavaBean
5.JavaBean:处理业务并操作数据库
6.JSP:将处理结果响应到浏览器呈现给用户
MVC处理过程
Spring MVC
7.用Controller替换JSP Model2模型中的Servlet
8.Controller收到请求后,完成业务处理并用Model模型对象存储处理结果
9.Controller调用相应的视图解析器View对处理结果进行视图渲染,最终客户端得到响应信息
二,使用SpringMvc
21,创建maven-web项目,修改webapp目录下WEB-INF目录下的web.xml文件
2.2,web.xml文件内容如下
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 页面中输入的中文,调到后端,出现乱码解决的问题 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.3,引入springMVC的依赖
<?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.xinxi2</groupId> <artifactId>SpringMvc1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>SpringMvc1 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> <spring.version>5.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- log4j 日志jar--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 连接mysql5 的驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!-- 参考版本对应 http://www.mybatis.org/spring/ --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 数据源管理 使用了dbcp2数据 --> <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.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- commons 文件上传jar --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 加入JSON转换工具 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies> <build> <finalName>SpringMvc1</finalName> </build> </project>
2.4,创建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.xsd"> <!-- 扫描 --> <context:component-scan base-package="com.controller"></context:component-scan> </beans>
2.5,创建一个controller类
import java.util.List; import java.util.Map; //标记该类为处理层类 @Controller @RequestMapping("/hello") public class HelloController { //把请求路径映射到该方法上 @RequestMapping("/hello01") public String hello01(){ System.out.println("hello01"); //请求转发到hello01.jsp界面 return "hello01.jsp"; } }
2.6,springMVC运行流程
- 客户端发生请求http://localhost:8080/springMVC17/hello
- 来到tomcat服务器。
- springmvc的前端控制器DipatcherServlet接受所有的请求。
- 查看你的请求地址和哪个@RequestMaping匹配。
- 执行对应的方法。方法会返回一个字符串。springmvc把该字符串解析为要转发的网页。
- 把该字符串经过视图解析器拼接。
- 拿到拼接的地址,找到对应的网页。
- 渲染该网页给客户
2.7,使用Model上传数据
@RequestMapping("/hello03") public String hello03(Student student,Model model){ System.out.println("hello03"+student.getUsername()+" "+student.getPwd()+" "+student.getBirthday()+" "+student.getAge()); model.addAttribute("linyuhao","hh"); return "hello01.jsp"; }
使用HttpServletRequest上传数据
@RequestMapping("/hello04") public String hello04(HttpServletRequest request){ System.out.println("hello04"); request.setAttribute("guangtou","gg"); return "hello01.jsp"; }
2.8,使用Map上传数据
@RequestMapping("/hello05") public String hello05(Map map){ System.out.println("hello05"); int count = 1/0; map.put("wangdao","qq"); return "redirect:/login.jsp"; }
2.9,静态资源加载问题
这样就需要我们在springMVC配置文件中将静态资源加载,加上
<!-- 解决了静态资源的加载问题 --> <mvc:resources mapping="/static/**" location="/static/"></mvc:resources>