简述springmvc的运行流程,springmvc案例

  简述springmvc的运行流程,springmvc案例

  

目录

springMVC程序实现1。什么是MVC工作流2。SpringMVCSpringMVC 3有什么特点。开发环境的准备。Hello world1的实际实施。项目2的创建。依赖关系介绍3。web.xml3的配置. Spring配置文件4的配置。请求控制器5的写入。页面文件6的写入。项目开始。

 

  

SpringMVC程序实现

 

  

一、什么是 MVC

MVC实际上是一种架构思想,将软件分为模型、视图和控制器。

 

  m:指的是Model,也就是模型层,工程上指的是JavaBean,功能是处理数据。V: View,即视图层,指的是项目中的html或jsp页面,其作用是与用户交互,显示数据。c:指项目中的控制器、控制层、servlet,用来接收请求,响应浏览器。M中的Javabean可以分为两类:

  实体Bean:专门存储业务数据,如学生、用户等。处理Bean:指服务或Dao对象,专门用于处理业务逻辑和数据访问。

  00-1010用户通过视图层向服务器发送请求,请求被服务器中的控制器接收。请求处理完成后,控制器调用相应的模型层处理请求,返回结果ControllerController,根据请求处理结果找到相应的view view,最后渲染数据后响应浏览器。

  

MVC 工作流程

SpringMVC是Spring的后续产品,也是Spring的子项目。SpringMVC是Spring为表示层的开发提供的一整套解决方案。

 

  目前业界普遍选择SpringMVC作为Java EE项目表示层开发的首选。

  

二、什么是 SpringMVC

Spring家族原生产品,无缝连接IOC容器等基础设施,基于原生Servlet,通过强大的前端控制器DispatcherServlet,统一处理请求和响应,全方位覆盖表示层各个细分领域要解决的问题,提供全面的解决方案。代码清新简洁。大大提高开发效率。内部组件化程度高。可插拔组件是即插即用的。如果想要什么功能,可以配置相应的性能出众的组件。特别适合现代大型、超大型互联网项目。

 

  

SpringMVC 的特点

实现举世闻名的程序之前需要准备好开发环境。

 

  IDE:idea 2018.3构建工具:maven3.5.4服务器:tomcat8Spring版本:5.3.1

  00-1010让我们一起做吧。

  00-1010这里有一个新项目。我把它命名为springmvc。然后在里面新建一个模块,叫springmvc-demo1,最后完成。

  请注意,添加web模块是在文件-项目结构中。

  00-1010在springmvc-demo1模块下的pom.xml文件中,需要引入相关的依赖关系。

  骑乘作物xml版本=1.0 编码=UTF-8 ?项目xmlns= http://maven . Apache . org/POM/4 . 0 . 0 xmlns : xsi= http://www . w3 . org/2001/XML schema-instance xsi : schema location= http://maven . Apache . org/POM/4 . 0 . 0 http://maven.apache.org/xsd/maven-4.0.0.xsd父artifactIdspringmvc/artifactId groupIdcom.pingguo.mvc/groupId版本1.0-快照/版本/父模型版本4 . 0 . 0/0

   <!--打包方式--> <packaging>war</packaging> <dependencies> <!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!-- 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring5和Thymeleaf整合包 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> </dependencies></project>

 

  

3. 配置 web.xml

在 webapp 下的 web.xml 中如下配置:

 

  

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 --> <init-param> <!-- contextConfigLocation为固定值 --> <param-name>contextConfigLocation</param-name> <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources --> <param-value>classpath:springMVC.xml</param-value> </init-param> <!-- 作为框架的核心组件,在启动过程中有大量的初始化操作要做 而这些操作放在第一次请求时才执行会严重影响访问速度 因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- 设置springMVC的核心控制器所能处理的请求的请求路径 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径 但是/不能匹配.jsp请求路径的请求 --> <url-pattern>/</url-pattern> </servlet-mapping></web-app>

 

  

3. 配置 spring 配置文件

在 resources 下新建 配置文件。

 

  

<?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" 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.pingguo.mvc.controller"></context:component-scan> <!-- 配置Thymeleaf视图解析器 --> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean></beans>

这里视图层使用 Thymeleaf,先不着急前后端分离,学习要一步步地来。

 

  

 

  

4. 编写请求控制器

由于前端控制器对浏览器发送的请求进行了统一的处理,但是具体的请求有不同的处理过程,因此需要创建处理具体请求的类,即请求控制器。

 

  请求控制器中每一个处理请求的方法成为控制器方法。

  因为SpringMVC的控制器由一个POJO(普通的Java类)担任,因此需要通过 @Controller 注解将其标识为一个控制层组件,交给 Spring 的 IoC容器管理,此时 SpringMVC 才能够识别控制器的存在。

  

package com.pingguo.mvc.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController { // @RequestMapping注解:处理请求和控制器方法之间的映射关系 // @RequestMapping注解的value属性可以通过请求地址匹配请求,/表示的当前工程的上下文路径 @RequestMapping(value = "/") public String index() { // 返回视图名称 return "index"; } @RequestMapping("/target") public String toTarget() { return "target"; }}

这里我直接加了 2 个方法,对应两个页面。

 

  

 

  

5. 编写页面文件

在 webapp/WEB-INF/templates 下,编写 html 文件。

 

  index.html

  

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8"> <title>首页</title></head><body><h1>Hello World</h1><a th:href="@{/target}" rel="external nofollow" >访问目标页面 target.html </a></body></html>

这里加了一个<a>标签,用来跳转到另一个页面 target.html。里面的th:href="@{/target}用的是 thymeleaf 里的语法,不用太过纠结与此,这不是本次学习的重点。因为后面最终还是会用前后端分离的方式进行应用的开发。

 

  target.html

  

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8"> <title>target</title></head><body><h1>target</h1></body></html>

 

  

6. 启动项目

因为项目打包方式是 war 包,部署在 tomcat 里,所以要先在本地安装个 tomcat,教程网上一大把。

 

  然后,在idea 中 add run configuration。

  先是 Deployment。

  

 

  这里的 Application context 就是应用上下文了,比如我要访问 /target,实际项目启动后在浏览器中访问的是:http://localhost:8080/springmvc/target 。

  接下来,就是 Server 配置了。

  

 

  启动,可以run 也可以 debug,debug 下可以用来断点调试。

  

 

  启动之后,默认会打开 http://localhost:8080/springmvc/。

  

 

  点击 index 页的 跳转连接,成功跳转到 target 页。

  

 

  再回顾下请求页面的过程:

  浏览器发送请求,若请求地址符合前端控制器的 url-pattern,该请求就会被前端控制器DispatcherServlet处理。前端控制器会读取 SpringMVC 的核心配置文件,通过扫描组件找到控制器,将请求地址和控制器中 @RequestMapping 注解的 value 属性值进行匹配,若匹配成功,该注解所标识的控制器方法就是处理请求的方法。处理请求的方法需要返回一个字符串类型的视图名称,该视图名称会被视图解析器解析,加上前缀和后缀组成视图的路径,通过 Thymeleaf 对视图进行渲染,最终转发到视图所对应页面。感谢《尚硅谷》的学习资源。

  以上就是世界著名程序SpringMVC完整过程的详细内容,更多关于SpringMVC程序实现的资料请关注盛行IT其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: