一,MVC 概述

MVC:模型,视图,控制器,是一种软件设计规范,本质是将业务逻辑,数据,显示,分离的方式来编写代码;前后端分离

Model:数据模型,提供要展示的数据,一般我们都会把这两个分离开来。数据Dao,服务层Service
View:负责进行数据的渲染和展示,客户端想要看到的东西
Controller:接收用户请求,交给Model处理,从Model更新后的数据或者结果,返回给前端

1,Model1

在早期 Java Web 的开发中,统一把显示层、控制层、数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1

Model1的弊端

  • JSP 和 Java Bean 之间严重耦合,Java 代码和 HTML 代码也耦合在了一起
  • 要求开发者不仅要掌握 Java ,还要有高超的前端水平
  • 前端和后端相互依赖,前端需要等待后端完成,后端也依赖前端完成,才能进行有效的测试
  • 代码难以复用

2,Model2

因为Model1的种种弊端,所以很快这种方式就被 Servlet + JSP + Java Bean 所替代了,首先用户的请求会到达 Servlet,然后根据请求调用相应的 Java Bean,并把所有的显示结果交给 JSP 去完成,这样的模式我们就称为 MVC 模式

controller(控制器)

  • 取得表单的数据

  • 调用业务的逻辑方法

  • 转向指定的页面

Model(模型)

  • Dao:操作数据库
  • Service:业务逻辑
  • 保存数据的更新状态

View(视图)

  • 网页, JSP,用来展示模型中的数据

二,SpringMVC 架构

SpringMVC 是 Spring的一部分,是基于Java实现的MVC的轻量级Web框架

SpringMVC优点

  • 趋势,使用的人多

  • 简单,易学,轻量级

  • 高效,基于请求和响应的MVC框架

  • 约定优于配置

  • 功能强大:RestFul,数据验证,格式化,主题,本地化,异常处理......

三,Hello SpringMVC

1,创建一个Maven项目

2,导入相关架包

 1 <dependencies>
2 <!--junit包单元测试-->
3 <dependency>
4 <groupId>junit</groupId>
5 <artifactId>junit</artifactId>
6 <version>4.11</version>
7 <scope>test</scope>
8 </dependency>
9
10 <!-- Spring MVC 及 Spring系列包 -->
11 <dependency>
12 <groupId>org.springframework</groupId>
13 <artifactId>spring-webmvc</artifactId>
14 <version>4.3.24.RELEASE</version>
15 </dependency>
16
17 <!--Servlet核心-->
18 <dependency>
19 <groupId>javax.servlet</groupId>
20 <artifactId>javax.servlet-api</artifactId>
21 <version>4.0.1</version>
22 </dependency>
23
24 <!-- JSTL -->
25 <dependency>
26 <groupId>javax.servlet</groupId>
27 <artifactId>jstl</artifactId>
28 <version>1.2</version>
29 </dependency>
30 </dependencies>
31
32 <build>
33 <!--解决资源导出问题-->
34 <resources>
35 <resource>
36 <directory>src/main/java</directory>
37 <includes>
38 <include>**/*.properties</include>
39 <include>**/*.xml</include>
40 </includes>
41 <filtering>false</filtering>
42 </resource>
43 <resource>
44 <directory>src/main/resources</directory>
45 <includes>
46 <include>**/*.properties</include>
47 <include>**/*.xml</include>
48 </includes>
49 <filtering>false</filtering>
50 </resource>
51 </resources>
52 </build>

3,配置web.xml,注册DispatcherServlet

映射路径为 /   【不要用/*,会404】

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6 id="WebApp_ID" version="3.0">
7
8 <!--1.注册DispatcherServlet-->
9 <servlet>
10 <servlet-name>SpringMVC</servlet-name>
11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
12
13 <!--2.关联SpringMVC配置文件-->
14 <init-param>
15 <param-name>contextConfigLocation</param-name>
16 <param-value>classpath:springmvc-servlet.xml</param-value>
17 </init-param>
18
19 <!--3.这个东西要和服务器一起启动-->
20 <load-on-startup>1</load-on-startup>
21 </servlet>
22
23 <servlet-mapping>
24 <servlet-name>SpringMVC</servlet-name>
25 <url-pattern>/</url-pattern>
26 </servlet-mapping>
27
28 </web-app>

4,配置springMVC的配置文件

在resources下创建springmvc-config.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="
7 http://www.springframework.org/schema/beans
8 https://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/context
10 https://www.springframework.org/schema/context/spring-context.xsd
11 http://www.springframework.org/schema/mvc
12 https://www.springframework.org/schema/mvc/spring-mvc.xsd">
13
14 <!--扫描指定包下的注解,让指定的类能够被IOC容器管理-->
15 <context:component-scan base-package="com.shandx.controller"/>
16
17 <!--静态资源过滤-->
18 <mvc:default-servlet-handler/>
19
20 <!--MVC注解驱动-->
21 <mvc:annotation-driven/>
22
23 <!--视图解析器-->
24 <bean id="InternalResourceViewResolver"
25 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26 <!-- 前缀 -->
27 <property name="prefix" value="/WEB-INF/"/>
28 <!-- 后缀 -->
29 <property name="suffix" value=".jsp"/>
30 </bean>
31 </beans>

5,编写Controller类

 1 //@Controller是为了让Spring IOC容器初始化时自动扫描到
2 @Controller
3 public class HelloWorldController {
4
5 //请求映射("路径")
6 @RequestMapping("/hello")
7 public String hi(Model model){
8 model.addAttribute("msg","Hello,SpringMVC");
9
10 //方法返回的结果是视图的名称hello
11 // 加上配置文件中的前后缀变成WEB-INF/hello.jsp。
12 return "hello"; //WEB-INF/jsp/hello.jsp
13 }
14 }

6,编写视图层

注意视图的位置,要和视图解析器对应 web-inf / jsp,可以通过EL表示取出Model中存放的值

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 <html>
3 <head>
4 <title>SpringMVC</title>
5 </head>
6 <body>
7
8 ${msg}
9
10 </body>
11 </html>

7,启动Tomcat进行测试

配置Tomcat

测试结果

四, 项目结构

最新文章

  1. KMP学习之旅
  2. Bootstrap之样式风格与下拉菜单
  3. mysql命令行导入sql脚本中文变问号问题
  4. world machine, 输出lightmap
  5. sublime text 快速补全
  6. 2016年31款轻量高效的开源JavaScript插件和库
  7. 【剑指offer 面试题17】合并两个排序的链表
  8. 高级UNIX环境编程3 FILE IO
  9. #pragma 预处理指令详解
  10. PL/SQL 游标的使用
  11. css3控制div上下跳动
  12. Java-NIO(五):通道(Channel)的数据传输与内存映射文件
  13. 【easy】561. Array Partition I
  14. springBoot 自动配置原理--自己新建一个 starter
  15. Django之信号和序列化
  16. PHP 汉字数字互转(100以内)| 汉字转数字 | 数字转汉字
  17. PHP安全过滤函数
  18. NAT概念解释(不完全版,但不会搞错...)
  19. 查看、生成 SSH 密钥用于安全登陆
  20. js 根据title从下级往上级查找

热门文章

  1. 攻防世界 Misc 新手练习区 如来十三掌 Writeup
  2. Flink 实践教程 - 入门(4):读取 MySQL 数据写入到 ES
  3. Git基本教程
  4. 解决create-react-app 后 npm start or yarn start 中出现 的webpack版本问题
  5. SpringCloud升级之路2020.0.x版-31. FeignClient 实现断路器以及线程隔离限流的思路
  6. 【JAVA】编程(1)---计算器
  7. 麒麟Linux上编译subversion
  8. [hdu5629]Clarke and tree
  9. [cf878D]Magic Breeding
  10. Go语言核心36讲(Go语言实战与应用十七)--学习笔记