DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servlet 3规范和Spring 3.1的功能增强, 可以采用一种全新的,更简洁的方式配置Spring MVC了. 下面按这种方式一个Hello World的MVC配置.
Step 1:先用eclipse创建一个Maven的WEB工程. pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ocr</groupId>
<artifactId>ocr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaee-api.version>7.0</javaee-api.version>
<spring.version>4.2.0.RELEASE</spring.version>
<junit.version>4.12</junit.version>
</properties> <dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 2: 配置DispatcherServlet. 需要创建一个Web初始化类OcrWebAppInitializer, 继承自AbstractAnnotationConfigDispatcherServletInitializer
package com.chry.ocr.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class OcrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class }; //ָ指定Web配置类
} @Override
protected String[] getServletMappings() { //将DispatcherServlet映射到"/"
return new String[] { "/" };
} }
Step 3: 配置Spring MVC视图解析WebConfig.java, 需要要创建一个类继承自WebMvcConfigurerAdapter
package com.chry.ocr.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration
@EnableWebMvc //启动SpringMVC
@ComponentScan("com.chry.ocr.controller") //启动组件扫描
public class WebConfig extends WebMvcConfigurerAdapter { //配置JSP视图解析器
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
} //配置静态资源的处理
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable(); //对静态资源的请求转发到容器缺省的servlet,而不使用DispatcherServlet
} }
Step 4: 配置RootConfig.java
package com.chry.ocr.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration
@ComponentScan( basePackages={"com.chry.ocr"},
excludeFilters = { @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}
) public class RootConfig { }
至此, 传统方式中需要通过web.xml进行配置的东西就已将全部完成有上面三个java类(OcrWebAppInitializer, RootConfig, WebConfig)完成. 可以开始写Controller和页面代码了
Step 5: 编写一个HomeController.java, 它将输出"hello World from Spring MVC"到home.jsp页面
package com.chry.ocr.controller; import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
public class HomeController {
@RequestMapping(value = "/", method=GET)
public ModelAndView home() {
String message = "Hello world from Spring MVC";
return new ModelAndView("home", "message", message);
}
}
Step 6: 编写一个jsp页面, 按照我们在视图解析器和Controller里面的配置,放在WEB-INF/views/home.jsp中
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Spring MVC Tutorial chry</title>
<style type="text/css">
</style>
</head>
<body>
<br>
<div style='text-align:center;'>
${message}
</div>
</body>
Step 7: 至此所有工作完成, 使用maven的"clean install"选项进行编译打包后,在执行,访问http://localhost:8080即可. 页面效果和工程结构如下,工程里面没有web.xml