3.1 配置build.gradle
plugins {
id 'java'
id 'war'
}
group 'org.springframework'
version '5.0.16.BUILD-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(project(":spring-beans"))
compile(project(":spring-core"))
optional(project(":spring-aop"))
optional(project(":spring-context"))
optional(project(":spring-oxm"))
optional(project(":spring-jdbc"))
optional(project(":spring-web"))
optional(project(":spring-webmvc"))
optional(project(":spring-test"))
optional group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
optional group: 'asm', name: 'asm', version: '3.3.1'
optional group: 'junit', name: 'junit', version: '4.12'
optional group: 'cglib', name: 'cglib', version: '2.2.2'
optional group: 'org.jsoup', name: 'jsoup', version: '1.11.3'
optional group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
optional group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'
optional("javax.annotation:javax.annotation-api:1.3.2")
optional("javax.ejb:javax.ejb-api:3.2")
optional("javax.enterprise.concurrent:javax.enterprise.concurrent-api:1.0")
optional("javax.inject:javax.inject:1")
optional("javax.interceptor:javax.interceptor-api:1.2.2")
optional("javax.money:money-api:1.0.3")
optional("javax.validation:validation-api:1.1.0.Final")
optional("javax.xml.ws:jaxws-api:2.3.0")
optional("org.aspectj:aspectjweaver:${aspectjVersion}")
optional("org.codehaus.groovy:groovy:${groovyVersion}")
optional("org.beanshell:bsh:2.0b5")
optional("joda-time:joda-time:2.10")
optional("org.hibernate:hibernate-validator:5.4.2.Final")
optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
testCompile("org.codehaus.groovy:groovy-xml:${groovyVersion}")
testCompile("org.codehaus.groovy:groovy-jsr223:${groovyVersion}")
testCompile("org.codehaus.groovy:groovy-test:${groovyVersion}")
testCompile("org.apache.commons:commons-pool2:2.6.0")
testCompile("javax.inject:javax.inject-tck:1")
testRuntime("javax.xml.bind:jaxb-api:2.3.0")
testRuntime("org.glassfish:javax.el:3.0.1-b08")
testRuntime("org.javamoney:moneta:1.3")
}
3.2 在resources资源文件下添加log4j.properties和spring-context.xml配置文件
log4j.properties:
# Output pattern : date [thread] priority category - message FATAL 0 ERROR 3 WARN 4 INFO 6 DEBUG 7
log4j.rootLogger=INFO, Console, RollingFile
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c{5}] - %m%n
#RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=mylog.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#log4j.logger.java.sql=DEBUG
#Springframework level
#log4j.logger.org.springframework=ERROR
#Hibernate level
#log4j.logger.org.hibernate=ERROR
#log4j.logger.org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory=ERROR
#log4j.logger.org.hibernate.search.impl.ConfigContext=ERROR
#log4j.logger.net.sf.ehcache.config.CacheConfiguration=ERROR
#Project defalult level
#log4j.logger.org.activiti.engine.impl.persistence=DEBUG
#log4j.logger.org.apache.shiro=DEBUG
log4j.logger.com.thinkgem.jeesite=DEBUG
log4j.logger.com.thinkgem.jeesite.common.security.shiro=WARN
log4j.logger.com.thinkgem.jeesite.common.utils.JedisUtils=WARN
log4j.logger.com.thinkgem.jeesite.modules.sys.web.LoginController=WARN
log4j.logger.com.thinkgem.jeesite.modules.oa.dao.OaNotifyDao.findCount=WARN
spring-context.xml:
3.3 在webapp下创建WEB-INF文件夹, 在WEB-INF下添加web.xml和spring-mvc-servlet.xml配置文件(spring-mvc为项目名)
web.xml:
spring-mvc
org.springframework.web.context.ContextLoaderListener
spring-mvc
org.springframework.web.servlet.DispatcherServlet
1
spring-mvc
/
contextConfigLocation
classpath:spring-context.xml
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/
spring-mvc-servlet.xml:
4.1 Model层
package com.lic.entity;
public class MyBean {
private int age;
private String name;
public MyBean(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyBean{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
4.2 View层, 在WEB-INF下新建pages文件夹并添加sayHello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
Insert title here
Hello world
姓名:${myBean.name}
年龄:${myBean.age}
4.2 Control层
package com.lic.controller;
import com.lic.entity.MyBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
@RequestMapping("sayHello")
@ResponseBody
public ModelAndView sayHello(int age, String name) {
ModelAndView mav = new ModelAndView();
MyBean bean = new MyBean(age, name);
mav.addObject("myBean", bean);
mav.setViewName("sayHello");
System.out.println(bean.toString());
return mav;
}
}
5.1 + -> TomcatServer -> local
5.2 启动程序命名 -> 选择Tomcat -> 设置项目名 -> 选择Java环境
5.3 设置项目名 Deployment -> Application context
5.4 浏览器输入地址: http://localhost:8080/spring-mvc/sayHello?age=12&name=lic
测试项目部署完成, 在该项目中引用的SpringMVC相关类都在源码项目中, 可以进行SpringMVC源码学习了!
相关文章:
SpringMVC源码解析一(在Spring源码项目中搭建SpringMVC源码模块)
SpringMVC源码解析二(请求过程解析)
SpringMVC源码解析三(处理映射器HandlerMapping的解析)
SpringMVC源码解析四(处理适配器HandlerAdapter的解析)
SpringMVC源码解析五(HandlerMethod执行过程解析)
SpringMVC源码解析六(ModelAndView解析)
SpringMVC源码解析七(初始化过程解析)