首页 > 编程语言 >Spring源码-SpringMVC-搭建springmvc环境

Spring源码-SpringMVC-搭建springmvc环境

时间:2022-10-29 21:11:33浏览次数:57  
标签:web tomcat SpringMVC Spring -- 源码 apache org log4j

一、新建模块myself-web
新建gradle的web项目,右键项目名,选择NEW-Moudle.

左边选择Gradle,右下选择web即可。

build.gradle

plugins {
id 'java'
id 'war'
id "com.bmuschko.tomcat" version "2.7.0"
}

group 'org.springframework'
version '5.3.6-SNAPSHOT'

repositories {
    maven { url 'https://maven.aliyun.com/repository/public/' }
    mavenLocal()
    mavenCentral()
    jcenter()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    implementation project(":spring-context")
    implementation project(":spring-beans")
    compile('org.aspectj:aspectjweaver:1.9.6')
    compile(project(":spring-web"))
    compile(project(":spring-webmvc"))

    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    implementation 'org.slf4j:slf4j-log4j12:1.7.32'
    implementation 'log4j:log4j:1.2.17'

    def tomcatVersion = '9.0.1'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:9.0.0.M6",
            "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

tomcat {
    httpPort = 8090
    httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
    ajpProtocol  = 'org.apache.coyote.ajp.AjpNio2Protocol'
}

test {
    useJUnitPlatform()
}

使用tomcat插件,地址是https://github.com/bmuschko/gradle-tomcat-plugin

二、项目结构

IndexController.java

@Controller
@RequestMapping("/")
public class IndexController {

	@RequestMapping("/main")
	public ModelAndView index() {
		Date date=new Date();
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("date",date);
		modelAndView.setViewName("main");
		return modelAndView;
	}

}

app-mvc.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 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="mvc.*"
							use-default-filters="true">
		<context:exclude-filter type="annotation"
								expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

</beans>

log4j.properties

log4j.rootLogger=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%5p [%t] (%F:%L) - %m%n

mvc.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"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<context:component-scan base-package="mvc.controller"/>

	<!-- 开启SpringMVC注解模式 -->
	<mvc:annotation-driven/>
	<!-- 静态资源默认servlet配置 -->
	<mvc:default-servlet-handler/>

<!--	<mvc:annotation-driven>-->
<!--		<mvc:message-converters>-->
<!--			<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>-->
<!--			<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>-->
<!--		</mvc:message-converters>-->
<!--	</mvc:annotation-driven>-->

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

</beans>

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">
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>

	<!-- web容器与spring上下文整合的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Spring和mybatis的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:app-mvc.xml</param-value>
	</context-param>

	<!-- 配置springmvc的前端控制器: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:mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

main.jsp

<%@ page language="java" isELIgnored="false" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
当前时间 ${date}
</body>
</html>

项目结构如下:

下图中

点击tomcatRunWar运行项目

浏览器访问http://localhost:8090/myself-web/main,结果如下

标签:web,tomcat,SpringMVC,Spring,--,源码,apache,org,log4j
From: https://www.cnblogs.com/shigongp/p/16839865.html

相关文章

  • xxl-job 源码初探
    xxl-job客户端把JobHandle的value和method映射关系存储到map中1.1启动入口1.2在该文件getBean后被调用1.3xxl-job实际映射位置1.4放置到map中......
  • 十三,SpringBoot-全局异常处理器
     springboot自定义拦截器,需要继承WebMvcConfigurerAdapter并重写addInterceptors。======以下仅为示例,代码沿用上一章=====具体实现如下:①创建MyInterceptor.java文件@Conf......
  • 解决SpringBoot测试提示Failed to resolve org.junit.platform:junit-platform-launch
    解决SpringBoot测试提示Failedtoresolveorg.junit.platform:junit-platform-launcher:1.5.2解决方案:只需要添加:junit-platform-launcher依赖即可<dependency>......
  • Springboot项目启动报错Failed to configure a DataSource: ‘url‘ attribute is not
    ***************************APPLICATIONFAILEDTOSTART***************************Description:FailedtoconfigureaDataSource:'url'attributeisnotspecified......
  • SpringMVC_day02
    SpringMVC_day02今日内容完成SSM的整合开发能够理解并实现统一结果封装与统一异常处理能够完成前后台功能整合开发掌握拦截器的编写1,SSM整合前面我们已经把Myba......
  • SpringMVC_day01
    SpringMVC_day01今日内容理解SpringMVC相关概念完成SpringMVC的入门案例学会使用PostMan工具发送请求和数据掌握SpringMVC如何接收请求、数据和响应结果掌握RESTfu......
  • Spring注解之@Value基于Apollo或者YAML文件为静态变量赋值
    摘要:SpringBoot微服务中,把在Apollo配置中心或者YAML文件里配置的属性赋值给静态变量。综述  Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同......
  • Springboot + bootstrap 实现 增删改查
    SpringBoot+bootstrap 配合mysql实现增删改查功能创建项目打开idea工具----  点击File---new---Project创建springBoot项目工程,版本统一:我使......
  • SpringBoot推送微信测试公众号信息
    1、登陆微信公众平台测试号2、扫码关注3、新建模版参数需以{{开头,以.DATA}}结尾,ex:{{msg.DATA}},代码里面替换就可以了templateMessage.addData(newWxMpTemplateDat......
  • String源码分析(四)
    ......