Spring MVC新手!花费三小时,居然直接上手搭建一个案例出来?

文章目录

  1. 什么是SpringMVC

在很久之前比较流行的架构模式有 SSH 即( Spring Struts 对servlet进行封装hibernate );–> 百度百科SSH框架

后来又出现了SSM( Spring Struts Mybatis ) ;注意这里还没有用 Spring MVC ,因为Spring早期发展时,Web模块并不是很好,所以这里web部分处理的话,早期用的是 Struts ;

再到后来,这个SSM框架说的就是( Spring Spring MVC Mybatis )组合体了;

现在再来说说这个 Spring MVC ;spring mvc是spring的一个模块;他们两个之间不需要什么中间层来连接;SpringMVC对servlet进行了封装;

springmvc 是一个基于 mvc 的 web 框架; 接收外部请求,解析参数传给服务层

其实就是,springmvc封装了servlet;接收客户端发送的请求,调用service服务层进行处理;再将处理的结果响应给客户端.


那么,再来看看啥是 MVC ;MVC的话,它是一个早期的开发架构思想;

由 M层(modle模型层) [包括pojo实体类,dao数据交互,service服务];

V层(view 视图层) [展示的页面,比如说jsp (实际还是servlet)];

C层(Controller 控制层) [放置servlet]




2.浅谈运行过程

初步接触,比较好理解的就是先上手开发使用一下,然后再慢慢剖析它的具体过程,学习效果会好一点.

可以把这个 DispatcherServlet 看作是个请求分发器;

首先让 DispatcherServlet接收请求后,调用处理映射器 HandlerMapping ;

这个映射器,它先去检查这个请求路径中有没有配拦截器,有的话过一遍;

然后这个处理映射器还会根据请求路径的url找到对应的 处 理 器 Handler(即Controller) 具体的控制层;把它返回给 DispatcherServlet ; DispatcherServlet 再次出发,根据 Handler 的找一个处理适配器;然后处理得到视图与模型,再返回到 DispatcherServlet ,进行分发处理;响应数据;

左下角 视图解析 那块儿写错字了;

不过,现在大量使用打印流的方式配合JSON格式发出响应数据的话,这个流程其实就相对比较简单了;具体的流程可以画成下面这样;




直接在控制层通过打印流的方式将数据信息响应了




3.上手搭建springmvc,进行使用

首先是导包

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.2.RELEASE</version> </dependency>

注意这个 spring-webmvc 包中其实就包含了 spring-context 的内容;所以我用了springmvc的jar包后就可以不用spring-context的包了;


这个案例是在上一篇笔记中的案例,spring整合mybatis后,添加配置文件搭配出来的;

Spring框架学习笔记(5) — [Spring框架整合Mybatis框架]

在 webapp 下的 WEB-INF 目录下的 web.xml 文件中,配置 DispatcherServlet;

<?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"> <!--DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> <!--启动时就创建servlet--> <load-on-startup>0</load-on-startup> </servlet> <!--servlet映射所有路径--> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

有没有注意到这里再使用初始参数时,要引用整合的spring配置文件;而且前面是classpath路径标注;由于到时候案例是要部署在tomcat服务器进行运行;资源访问路径要指向正确;

那么之前写的几个引用文件路径前也要加这个哦;实际上一篇笔记写这个案例的时候,已经加了;这里提醒一下,加深记忆.

spring-dbandtx.xml




spring-mybatis.xml


然后就是去配置开启MVC的注解扫描;

<mvc:annotation-driven></mvc:annotation-driven>

在 resources 目录下新建 spring-mvc.xml ,放置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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启springmvc注解支持--> <mvc:annotation-driven></mvc:annotation-driven> </beans>

这里在 spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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.xiaozhire0.ssm"></context:component-scan> <!--合并数据连接以及事务管理配置文件--> <import resource="spring-dbandtx.xml"></import> <!--合并spring整合mybatis的配置文件--> <import resource="spring-mybatis.xml"></import> <!--合并springmvc配置文件--> <import resource="spring-mvc.xml"></import> </beans>

控制器类搭建;

使用 @Controller 注解标注 类为springmvc控制类对象 ;

或者使用注解 @RestController ;因为它包含了注解 @Controller 的功能




可使用@RequestMapping(path="/ …")标注对应的请求路径

我这里就在ssm包下创建controller包,创建 PersonController ;

/**
 * @author by CSDN@小智RE0
 * @date 2021-11-21 16:51
 * 人类控制层;
 */ //标注此类为控制类; @RestController @RequestMapping(path = "/person") public class PersonController { //自动装配服务层; @Autowired PersonService personService; //调用这个方法的访问路径其实就是 项目/person/save @RequestMapping(path = "/save") public void toSavePeople(){
 
       System.out.println("访问到添加用户的路径---->");
        People people = new People();
        people.setName("从零开始的异世界Java开发");
        people.setPassword("487945");
        people.setAge(18);
        people.setBirthday(new Date());
        personService.savePeople(people);
    }
}

配置tomcat服务器;

启动测试;

这里还犯了了一个错误;把包路径建错了;然后启动服务器一直报错;

报的是这种错误

21-Nov-2021 18:30:12.796 警告 [http-nio-8080-exec-4] 
org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET
 ;

然后我就一个一个检查配置文件,重新部署服务器,maven清理重新编译;都没找到错误;

裂开,排错找了好久,才发现之前把包建错位置了.


访问路径 
http://localhost:8080/ssmdemobyxiaozhi/person/save
 后;执行成功



全部评论

相关推荐

07-18 14:55
门头沟学院 Java
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务