• Spring和Spring Mvc整合详解


    Spring和Spring Mvc整合详解

    官方主页

    Spring

    Spring Mvc

    SpringMvc 5,可以参考这一篇《Spring和Spring Mvc 5整合详解》

    概述

    Spring Mvc的启动方式不同于Spring Boot,Spring Boot内嵌了tomcat容器,可以打包成jar文件快速启动。Spring Mvc仍需要打包成war包。所以,它是离不开web.xml配置。

    配置Spring和Spring Mvc,主要有:

    • 1.在web.xml中配置好Spring相关Listener/Filter/Servlet,并指明Spring和Spring Mvc的配置文件,当然,也可以不指定,放在classpath下就行,严谨一点还是写出来为好。
    • 2.配置applicationContext.xml,这个是给Spring用的,名字随意,只要在web.xml指定就行。
    • 3.配置 spring-servlet.xml,这个是给Spring Mvc用的,名字随意,只要在web.xml指定就行。

    Git地址:
    Gitee

    项目地址:
    品茗IT-同步发布

    品茗IT 提供在线支持:

    一键快速构建Spring项目工具

    一键快速构建SpringBoot项目工具

    一键快速构建SpringCloud项目工具

    一站式Springboot项目生成

    如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。

    开始搭建

    依赖Jar包

    
    	org.springframework
    	spring-core
    	${spring.version}
    
    
    	org.springframework
    	spring-context
    	${spring.version}
    
    
    	org.springframework
    	spring-beans
    	${spring.version}
    
    
    	org.springframework
    	spring-web
    	${spring.version}
    
    
    	org.springframework
    	spring-webmvc
    	${spring.version}
    
    
    • 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

    web.xml

    
    
      UmpEnt
      
        log4jConfigLocation
        classpath:properties/log4j.properties
      
      
        log4jRefreshInterval
        600000
      
      
        org.springframework.web.util.Log4jConfigListener
      
      
        org.springframework.web.context.ContextLoaderListener
      
      
        contextConfigLocation
        classpath:applicationContext.xml
      
      
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
          encoding
          UTF-8
        
        
          forceEncoding
          true
        
      
      
        characterEncodingFilter
        /*
      
      
        springSecurityFilterChain
        org.springframework.web.filter.DelegatingFilterProxy
      
      
        springSecurityFilterChain
        /*
      
      
        springDispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
          contextConfigLocation
          classpath:spring-servlet.xml
        
        1
      
      
        springDispatcherServlet
        /
      
      
        /index.html
      
    
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    Spring配置

    
    
    
    	
    	
    	
    	
    	
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    这个配置文件里可以写入一些bean的配置。

    Spring是一个大的父容器,Spring Mvc是其中的一个子容器。父容器不能访问子容器对象,但是子容器可以访问父容器对象。 因此,bean的配置要写到这个文件中,而不是Spring Mvc的配置文件中。

    Spring Mvc配置

    
    
    
    	
    	
    	
    	
    		
    		
    	
    
    	
    	
    
    	
    
    	
    		
    			
    				
    				
    					
    						
    							application/json;charset=UTF-8
    						
    					
    				
    			
    		
    	
    	
    
    	
    		
    			
    			
    		
    	
    
    	
    	
    	
    	
    
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    Spring是一个大的父容器,Spring Mvc是其中的一个子容器。父容器不能访问子容器对象,但是子容器可以访问父容器对象。 因此,bean的配置要写到这个文件中,而不是Spring Mvc的配置文件中。

    简单的Controller

    package com.cff.springwork.web.endpoint.test;
    
    import java.util.UUID;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.cff.springwork.web.entity.WelEntity;
    
    @RestController("testController")
    @RequestMapping("/test")
    public class TestController {
    
    	@RequestMapping("/welCome")
    	public WelEntity welCome(@RequestParam String reqType){
    		String uuid = UUID.randomUUID().toString();
    		String welMsg = "welcome 程序猿";
    		if(reqType != null && "1000".equals(reqType)){
    			welMsg = "welcome 程序媛";
    		}
    		WelEntity welEntity = new WelEntity();
    		welEntity.setUuid(uuid);
    		welEntity.setWelMsg(welMsg);
    		return welEntity;
    	}
    }
    
    • 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

    实体:

    package com.cff.springwork.web.entity;
    
    public class WelEntity {
    	private String uuid;
    	private String welMsg;
    	public String getUuid() {
    		return uuid;
    	}
    	public void setUuid(String uuid) {
    		this.uuid = uuid;
    	}
    	public String getWelMsg() {
    		return welMsg;
    	}
    	public void setWelMsg(String welMsg) {
    		this.welMsg = welMsg;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    前端配合

    
    
    
    
    
    SkyNet
    
    
    
    	这是html
    	
    • 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
    • 37

    快速构建项目

    Spring组件化构建

    喜欢这篇文章么,喜欢就加入我们一起讨论Spring技术吧!

  • 相关阅读:
    leetcode 32. 最长有效括号
    基于springboot 手工艺品在线展示系统-计算机毕设 附源码 42553
    电脑如何激活windows
    python常用知识梳理(必看篇)
    数据可视化——根据提供的数据,将数据经过处理后以折线图的形式展现
    激活函数介绍
    Vue学习
    CORBA 架构体系指南(通用对象请求代理体系架构)​
    阿里云轻量服务器使用一年使用体验(个人心得,仅供参考)
    Ansible自动化:简化IT基础设施管理的艺术
  • 原文地址:https://blog.csdn.net/m0_67401382/article/details/126516514