• [springmvc]springmvc的实现流程原理,以及快速创建一个三层架构项目


    1.回顾mvc

    模型

    • 模型(dao,service):业务逻辑,保存数据的状态

    视图

    • 视图(jsp,html……):展示页面

    控制器

    • 控制(servlet):取得表单数据,调用业务逻辑,转向指定的页面

    2.认识springMvc

    • 轻量级,简单易学
    • 高效,基于请求响应
    • 与spring可以无缝结合
    • 约定优于配置
    • 功能强大:restful风格,数据验证,格式化,本地化
    • 简洁灵活

    image-20220725110738893

    在以前的学习中,我们总是根据不同的需求请求不同的servlet,那么对于用户来说,这一步骤就非常麻烦,要不断的转换请求,为了解决这一问题,在原有的架构基础上面加上一层,专门用来处理这些请求,用户只需要请求 上面的一层即可。它就是DispatcherServlet

    实际上这个DispatcherServlet它本身也是一个servlet。

    实现原理:

    image-20220725113446190

    image-20220725113752138

    快速搭建一个springmvc项目

    方式一 实现类

    (1)配置web文件

    
    <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">
    
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:springmvc-servlet.xmlparam-value>
            init-param>
    
            
            <load-on-startup>1load-on-startup>
    
        servlet>
    
        
    
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
    
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    (2)配置spring核心文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    
        
        <bean name="/firstController" class="com.spring.controller.HelloController"/>
        
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
    
        <bean id="viewResolver"    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
    
        bean>
    
    
        <bean id="/h1" class="com.spring.controller.HelloController"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    (3)调用控制器层跳转

    package com.spring.controller;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * @author panglili
     * @create 2022-07-25-12:01
     */
    public class HelloController implements Controller {
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            ModelAndView view = new ModelAndView();
    
            //业务代码
            String result="HelloSpringMvc";
            view.addObject("msg",result);
            //视图跳转
            view.setViewName("WEB-INF/jsp/test.jsp");
    
            return view;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    (4)视图层接受数据传给用户

    <%--
      Created by IntelliJ IDEA.
      User: 塔塔
      Date: 2022/7/25
      Time: 13:22
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    ${msg}
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    image-20220725162431627

    方式二 注解实现

    (1)配置web文件

    • 跟上面实现类的相同

    (2)配置spring文件

    
    <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-4.3.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        <context:component-scan base-package="com.spring.controller"/>
        <mvc:default-servlet-handler/>
        <mvc:annotation-driven/>
    
        
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    (3)控制器类实现界面跳转

    package com.spring.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author panglili
     * @create 2022-07-25-13:15
     */
    @Controller
    public class hello {
    
        @RequestMapping("/hello")
        public String hello(Model md){
            md.addAttribute("msg","helloooo");
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (4)视图跳转

    • 同实现类相同
  • 相关阅读:
    Kafka生产者之分区
    《菜狗商城》Springboot+Vue电商项目
    A-深度学习面试题
    前端面试之前端工程化篇
    java计算机毕业设计ssm基金分析系统的设计与实现
    算法学习 【第二周】贪心算法实例 Ⅱ
    氨基酸代谢:从基础到应用,揭示其在健康与疾病的角色
    Kafka原理剖析之「位点提交」
    容器云安全挑战和攻防应对
    What I Read(1) 地理空间数据库原理(A) 绪论
  • 原文地址:https://blog.csdn.net/weixin_48595687/article/details/126126562