Skip to content

Commit a650343

Browse files
committed
Spring MVC 零配置实现
1 parent 415677b commit a650343

File tree

7 files changed

+304
-0
lines changed

7 files changed

+304
-0
lines changed

pom.xml

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.chenfeng.xiaolyuh</groupId>
5+
<artifactId>spring-mvc-student</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
9+
<properties>
10+
<!-- Generic properties -->
11+
<java.version>1.8</java.version>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
<!-- Web -->
15+
<jsp.version>2.2</jsp.version>
16+
<jstl.version>1.2</jstl.version>
17+
<servlet.version>3.1.0</servlet.version>
18+
<!-- Spring -->
19+
<spring-framework.version>4.1.5.RELEASE</spring-framework.version>
20+
<!-- Logging -->
21+
<logback.version>1.0.13</logback.version>
22+
<slf4j.version>1.7.5</slf4j.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>javax</groupId>
28+
<artifactId>javaee-web-api</artifactId>
29+
<version>7.0</version>
30+
<scope>provided</scope>
31+
</dependency>
32+
33+
<!-- Spring MVC -->
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-webmvc</artifactId>
37+
<version>${spring-framework.version}</version>
38+
</dependency>
39+
40+
<!-- Spring and Transactions -->
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-tx</artifactId>
44+
<version>${spring-framework.version}</version>
45+
</dependency>
46+
47+
<!-- 其他web依赖 -->
48+
<dependency>
49+
<groupId>javax.servlet</groupId>
50+
<artifactId>jstl</artifactId>
51+
<version>${jstl.version}</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>javax.servlet</groupId>
56+
<artifactId>javax.servlet-api</artifactId>
57+
<version>${servlet.version}</version>
58+
<scope>provided</scope>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>javax.servlet.jsp</groupId>
63+
<artifactId>jsp-api</artifactId>
64+
<version>${jsp.version}</version>
65+
<scope>provided</scope>
66+
</dependency>
67+
68+
<!-- 使用SLF4J和LogBack作为日志 -->
69+
<dependency>
70+
<groupId>org.slf4j</groupId>
71+
<artifactId>slf4j-api</artifactId>
72+
<version>${slf4j.version}</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>log4j</groupId>
76+
<artifactId>log4j</artifactId>
77+
<version>1.2.16</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.slf4j</groupId>
81+
<artifactId>jcl-over-slf4j</artifactId>
82+
<version>${slf4j.version}</version>
83+
</dependency>
84+
<dependency>
85+
<groupId>ch.qos.logback</groupId>
86+
<artifactId>logback-classic</artifactId>
87+
<version>${logback.version}</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>ch.qos.logback</groupId>
91+
<artifactId>logback-core</artifactId>
92+
<version>${logback.version}</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>ch.qos.logback</groupId>
96+
<artifactId>logback-access</artifactId>
97+
<version>${logback.version}</version>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.logback-extensions</groupId>
101+
<artifactId>logback-ext-spring</artifactId>
102+
<version>0.1.4</version>
103+
</dependency>
104+
105+
106+
<!--对json和xml格式的支持 -->
107+
<dependency>
108+
<groupId>com.fasterxml.jackson.dataformat</groupId>
109+
<artifactId>jackson-dataformat-xml</artifactId>
110+
<version>2.5.3</version>
111+
</dependency>
112+
113+
<!-- file upload -->
114+
<dependency>
115+
<groupId>commons-fileupload</groupId>
116+
<artifactId>commons-fileupload</artifactId>
117+
<version>1.3.1</version>
118+
</dependency>
119+
120+
<!-- 非必需,可简化IO操作 -->
121+
<dependency>
122+
<groupId>commons-io</groupId>
123+
<artifactId>commons-io</artifactId>
124+
<version>2.3</version>
125+
</dependency>
126+
127+
<!-- 测试 -->
128+
<dependency>
129+
<groupId>org.springframework</groupId>
130+
<artifactId>spring-test</artifactId>
131+
<version>${spring-framework.version}</version>
132+
<scope>test</scope>
133+
</dependency>
134+
<dependency>
135+
<groupId>junit</groupId>
136+
<artifactId>junit</artifactId>
137+
<version>4.11</version>
138+
<scope>test</scope>
139+
</dependency>
140+
141+
</dependencies>
142+
143+
<build>
144+
<plugins>
145+
<plugin>
146+
<groupId>org.apache.maven.plugins</groupId>
147+
<artifactId>maven-compiler-plugin</artifactId>
148+
<version>2.3.2</version>
149+
<configuration>
150+
<source>${java.version}</source>
151+
<target>${java.version}</target>
152+
</configuration>
153+
</plugin>
154+
<plugin>
155+
<groupId>org.apache.maven.plugins</groupId>
156+
<artifactId>maven-war-plugin</artifactId>
157+
<version>2.3</version>
158+
<configuration>
159+
<failOnMissingWebXml>false</failOnMissingWebXml>
160+
</configuration>
161+
</plugin>
162+
</plugins>
163+
</build>
164+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.chenfeng.xiaolyuh.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
8+
import org.springframework.web.servlet.view.JstlView;
9+
10+
/**
11+
* MVC 配置类。这里我们配置了一个jsp的ViewResolver,用来映射路径和实际页面的位置,
12+
* 其中@EnableWebMvc注解会开启一些默认的配置,如一些ViewResolver或者MessageConverter
13+
* @author yuhao.wang
14+
* @Date 2017年3月29日 下午3:41:20
15+
*/
16+
@Configuration
17+
@EnableWebMvc
18+
@ComponentScan("com.chenfeng.xiaolyuh")
19+
public class MvcConfig {
20+
21+
@Bean
22+
public InternalResourceViewResolver viewResolver() {
23+
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
24+
viewResolver.setPrefix("/WEB-INF/classes/views/");// 运行时代码会将页面自动编译到/WEB-INF/classes/views/下
25+
viewResolver.setSuffix(".jsp");
26+
viewResolver.setViewClass(JstlView.class);
27+
return viewResolver;
28+
}
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.chenfeng.xiaolyuh.config;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.ServletRegistration.Dynamic;
6+
7+
import org.springframework.web.WebApplicationInitializer;
8+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
9+
import org.springframework.web.servlet.DispatcherServlet;
10+
11+
import ch.qos.logback.ext.spring.web.LogbackConfigListener;
12+
/**
13+
* web配置类。程序的入口
14+
* WebApplicationInitializer类是Spring提供用来配置Servlet3.0+配置的接口,从而实现了代替web.xml的位置。
15+
* 实现此接口将会自动被SpringServletContainerInitializer获取到,SpringServletContainerInitializer用来启动Servlet3.0容器。
16+
* 容器启动时会自动扫描当前服务中ServletContainerInitializer的实现类,并调用其onStartup方法,
17+
* 其参数Set<Class<?>> c,可通过在实现类上声明注解javax.servlet.annotation.HandlesTypes(xxx.class)注解自动注入,
18+
* @HandlesTypes会自动扫描项目中所有的xxx.class的实现类,并将其全部注入Set。
19+
* @author yuhao.wang
20+
*/
21+
public class WebInitializer implements WebApplicationInitializer{
22+
23+
@Override
24+
public void onStartup(ServletContext servletContext) throws ServletException {
25+
// 新建一个WebApplicationContext的注册配置类,并和当前的servletContext关联
26+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
27+
context.register(MvcConfig.class);
28+
context.setServletContext(servletContext);
29+
30+
// 注册Spring mvc的DispatchServlet
31+
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
32+
servlet.addMapping("/");
33+
servlet.setLoadOnStartup(1);
34+
35+
// 日志配置
36+
servletContext.setInitParameter("logbackConfigLocation", "classpath:logback.xml");
37+
servletContext.addListener(LogbackConfigListener.class);
38+
}
39+
40+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.chenfeng.xiaolyuh.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
@Controller
7+
public class HelloController {
8+
@RequestMapping("index")
9+
public String hello() {
10+
return "index";
11+
}
12+
}

src/main/resources/logback.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration scan="true" scanPeriod="1 seconds">
3+
4+
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
5+
<resetJUL>true</resetJUL>
6+
</contextListener>
7+
8+
<jmxConfigurator />
9+
10+
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
11+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
12+
<fileNamePattern>E:/workspace/spring-ecosystem-student/spring-mvc-student/logs/log.%d{yyyy-MM-dd}.log</fileNamePattern>
13+
<maxHistory>30</maxHistory>
14+
</rollingPolicy>
15+
<encoder>
16+
<pattern>%-4relative %d{HH:mm:ss.SSS} - [%thread] %-5level %class - %msg%n</pattern>
17+
</encoder>
18+
</appender>
19+
20+
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
21+
<encoder>
22+
<pattern>logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
23+
</encoder>
24+
</appender>
25+
26+
<!-- 将org.springframework.web包下的类的日志级别设置为DEBUG,
27+
我们开发Spring MVC经常出现和参数类型相关的4XX错误,设置此项我们可以看到更相信的信息 -->
28+
<logger name="org.springframework.web" level="DEBUG" />
29+
30+
<root level="debug">
31+
<appender-ref ref="console" />
32+
<appender-ref ref="file" />
33+
</root>
34+
</configuration>

src/main/resources/views/index.jsp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>Insert title here</title>
8+
</head>
9+
<body>
10+
<pre>
11+
Welcome to Spring MVC world
12+
</pre>
13+
</body>
14+
</html>

src/main/webapp/index.jsp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6+
<title>Insert title here</title>
7+
</head>
8+
<body>
9+
Hello World!
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)