Spring源码-SpringMVC-搭建springmvc环境
一、新建模块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
<context:component-scan base-package="mvc.*"
use-default-filters="true">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
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
<context:component-scan base-package="mvc.controller"/>
mvc:annotation-driven/
mvc:default-servlet-handler/
web.xml
main.jsp
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
当前时间 ${date}
项目结构如下:

下图中

点击tomcatRunWar运行项目

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


浙公网安备 33010602011771号