栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring和Spring Mvc整合详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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}

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
  

Spring配置



	
	
	
	
	

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

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

Spring Mvc配置



	
	
	
	
		
		
	

	
	

	

	
		
			
				
				
					
						
							application/json;charset=UTF-8
						
					
				
			
		
	
	

	
		
			
			
		
	

	
	
	
	

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;
	}
}

实体:

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;
	}
}
前端配合





SkyNet



	这是html
	
	
	


快速构建项目

Spring组件化构建

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号