Step 1:先用eclipse创建一个Maven的WEB工程. pom.xml文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Step 2: 配置DispatcherServlet. 需要创建一个Web初始化类OcrWebAppInitializer, 继承自AbstractAnnotationConfigDispatcherServletInitializer
1 package com.chry.ocr.config;
2
3 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4
5 public class OcrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6
7 @Override
8 protected Class>[] getRootConfigClasses() {
9 return new Class>[] { RootConfig.class };
10 }
11
12 @Override
13 protected Class>[] getServletConfigClasses() {
14 return new Class>[] { WebConfig.class }; //ָ指定Web配置类
15 }
16
17 @Override
18 protected String[] getServletMappings() { //将DispatcherServlet映射到"/"
19 return new String[] { "/" };
20 }
21
22 }
Step 3: 配置Spring MVC视图解析WebConfig.java, 需要要创建一个类继承自WebMvcConfigurerAdapter
1 package com.chry.ocr.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.ViewResolver;
7 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
8 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10 import org.springframework.web.servlet.view.InternalResourceViewResolver;
11
12 @Configuration
13 @EnableWebMvc //启动SpringMVC
14 @ComponentScan("com.chry.ocr.controller") //启动组件扫描
15 public class WebConfig extends WebMvcConfigurerAdapter {
16
17 //配置JSP视图解析器
18 @Bean
19 public ViewResolver viewResolver() {
20 InternalResourceViewResolver resolver = new InternalResourceViewResolver();
21 resolver.setPrefix("/WEB-INF/views/");
22 resolver.setSuffix(".jsp");
23 resolver.setExposeContextBeansAsAttributes(true);
24 return resolver;
25 }
26
27 //配置静态资源的处理
28 @Override
29 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
30 configurer.enable(); //对静态资源的请求转发到容器缺省的servlet,而不使用DispatcherServlet
31 }
32
33 }
Step 4: 配置RootConfig.java
1 package com.chry.ocr.config;
2
3 import org.springframework.context.annotation.ComponentScan;
4 import org.springframework.context.annotation.ComponentScan.Filter;
5 import org.springframework.context.annotation.Configuration;
6 import org.springframework.context.annotation.FilterType;
7 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8
9 @Configuration
10 @ComponentScan( basePackages={"com.chry.ocr"},
11 excludeFilters = { @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}
12 )
13
14 public class RootConfig {
15
16 }
至此, 传统方式中需要通过web.xml进行配置的东西就已将全部完成有上面三个java类(OcrWebAppInitializer, RootConfig, WebConfig)完成. 可以开始写Controller和页面代码了
Step 5: 编写一个HomeController.java, 它将输出"hello World from Spring MVC"到home.jsp页面
1 package com.chry.ocr.controller;
2
3 import static org.springframework.web.bind.annotation.RequestMethod.*;
4 import org.springframework.stereotype.Controller;
5 import org.springframework.web.bind.annotation.RequestMapping;
6 import org.springframework.web.bind.annotation.RequestMethod;
7 import org.springframework.web.servlet.ModelAndView;
8
9 @Controller
10 public class HomeController {
11 @RequestMapping(value = "/", method=GET)
12 public ModelAndView home() {
13 String message = "Hello world from Spring MVC";
14 return new ModelAndView("home", "message", message);
15 }
16 }
Step 6: 编写一个jsp页面, 按照我们在视图解析器和Controller里面的配置,放在WEB-INF/views/home.jsp中
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>
2 6
8
9