SpringMVC 总结之创建第一个SpringMVC项目
反思反问:
1.什么是MVC?三层架构是什么?
2.MVC 与三层架构区别是什么?
3.SpringMVC 帮我们做了哪些事?
4.SpringMVC主要核心是什么?
5. SpringMVC 关键的配置文件有哪些?里面的关键配置参数有哪些?
6.创建项目注意事项和流程有哪些?
理解MVC与三层架构
1.MVC是一种软件架构设计模式,主要分为三层:model(模型)(主要业务和数据收集) view(视图 数据展示) controller(控制器 负责调度 是一个调度中心)
主要优点:低耦合,高内聚,扩展强,维护性高,代码复用性强。
MVC架构模式如下所示: MVC架构模式的描述:前端浏览器发送请求给web服务器,web服务器中的Controller接收到用户的请求,Controller负责将前端提交的数据进行封装,然后Controller调用Model来处理业务,当Model处理完业务后会返回处理之后的数据给Controller,Controller再调用View来完成数据的展示,最终将结果响应给浏览器,浏览器进行渲染展示页面。
2.三层架构
三层架构主要分为:
表示层/界面层/表现层:展示处理数据
业务层(service):处理业务逻辑
持久层(dao):访问数据库将数据持久化。
3.MVC与三层架构的区别
MVC 和三层模型都采用了分层结构来设计应用程序,都是降低耦合度,提高扩展力,提高组件复用性。
区别在于:他们的关注点不同,三层模型更加关注业务逻辑组件的划分。
MVC架构模式关注的是整个应用程序的层次关系和分离思想。现代的开发方式大部分都是MVC架构模式结合三层模型一起用。
(巧记:同分层,老三重业务,老M重整体层次,注重分层隔离)。
什么是SpringMVC
SpringMVC是一个基于servlet实现MVC架构的web轻量级框架,SpringMVC是SpringWeb 的一个子模块,可以说SpringMVC是Spring框架的一部分。
SpringMVC做了哪些工作
SpringMVC框架帮我们做了什么,与纯粹的Servlet开发有什么区别?
- 入口控制:SpringMVC框架通过DispatcherServlet作为入口控制器,负责接收请求和分发请求。而在Servlet开发中,需要自己编写Servlet程序,并在web.xml中进行配置,才能接受和处理请求。
- 在SpringMVC中,表单提交时可以自动将表单数据绑定到相应的JavaBean对象中,只需要在控制器方法的参数列表中声明该JavaBean对象即可,无需手动获取和赋值表单数据。而在纯粹的Servlet开发中,这些都是需要自己手动完成的。
- IoC容器:SpringMVC框架通过IoC容器管理对象,只需要在配置文件中进行相应的配置即可获取实例对象,而在Servlet开发中需要手动创建对象实例。
- 统一处理请求:SpringMVC框架提供了拦截器、异常处理器等统一处理请求的机制,并且可以灵活地配置这些处理器。而在Servlet开发中,需要自行编写过滤器、异常处理器等,增加了代码的复杂度和开发难度。
- 视图解析:SpringMVC框架提供了多种视图模板,如JSP、Freemarker、Velocity等,并且支持国际化、主题等特性。而在Servlet开发中需要手动处理视图层,增加了代码的复杂度。
总之,与Servlet开发相比,SpringMVC框架可以帮我们节省很多时间和精力,减少代码的复杂度,更加专注于业务开发。同时,也提供了更多的功能和扩展性,可以更好地满足企业级应用的开发需求。
SpringMVC的核心
DispatcherServlet
DispatcherServlet 继承了父类FrameworkServlet
ViewResolver 视图解析器:可以理解为解析html,jsp页面的一个接口.不配置无法解析处理页面.
HandlerAdapter
适配器:帮助dispatchServlet 找到合适的controller或者调用合适组件.
HttpMessageConvert
HttpMessageConverter是Spring MVC中非常重要的一个接口。翻译为:HTTP消息转换器。该接口下提供了很多实现类,不同的实现类有不同的转换方式。
创建第一个SpringMVC
-
目录结构
-
引入相关依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.24</version> </dependency> </dependencies> <build> <finalName>SpringMvc03</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build>
-
编写springmvc配置文件
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 添加包扫描 --> <context:component-scan base-package="com.dqw.controller"></context:component-scan> <!-- 模板解析器 --> <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/"></property> <property name="suffix" value=".html"></property> <property name="templateMode" value="HTML"></property> <property name="cacheable" value="false"></property> <property name="characterEncoding" value="UTF-8"></property> </bean> <!-- 模板引擎 --> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"></property> </bean> <!-- thymeleaf 视图解析器 --> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"/> <property name="characterEncoding" value="UTF-8"/> </bean> <!-- 注册驱动 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 释放静态资源 --> <mvc:default-servlet-handler></mvc:default-servlet-handler> </beans>
-
编写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> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
编写第一个controller
@Controller public class FirstController { @RequestMapping("/ha") public String haha(){ return "first"; } }
-
配置部署到tomcat当中
略
7.访问网址查看测试结果