基于XML搭建SpringMVC项目

*如果你需要将应用部署到不支持Servlet3.0容器中 或者 你只是对web.xml情有独钟,那我们只能按照传统的方式,通过web.xml来配置SpringMVC。

*搭建SpringMVC需要在web.xml中注册DispatcherServlet和ContextLoaderListener,同时他们两个之间会分别通过上下文参数contextConfigLocation指定一个XML文件地址来加载Spring应用上下文,而我更倾向于使用Java配置类来加载Spring应用上下文(本文也是如此)。

 

(1)pom中添加SpringMVC相关jar

1     <dependency>
2       <groupId>org.springframeworkgroupId>
3       <artifactId>spring-webmvcartifactId>
4       <version>4.3.21.RELEASEversion>
5     dependency>

(2)设置web.xml文件(使用Java配置类来加载Spring应用上下文)

*要在SpringMVC中使用Java配置类来构建Spring应用上下文,我们需要配置DispatcherServlet和ContextLoaderListener的contextClass上下文参数为AnnotationConfigWebApplicationContext,AnnotationConfigWebApplicationContext是WebApplicationContext的实现类,它会加载Java配置类而不是XML文件

 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xmlns="http://java.sun.com/xml/ns/javaee"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5          id="WebApp_ID" version="2.5">
 6     
 7     <context-param>
 8         <param-name>contextClassparam-name>
 9         <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContextparam-value>
10     context-param>
11     
12     <context-param>
13         <param-name>contextConfigLocationparam-name>
14         <param-value>cn.coreqi.config.RootConfigparam-value>
15     context-param>
16     
17     <listener>
18         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
19     listener>
20     
21     <servlet>
22         <servlet-name>dispatcherServletservlet-name>
23         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
24         
25         <init-param>
26             <param-name>contextClassparam-name>
27             <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContextparam-value>
28         init-param>
29         
30         <init-param>
31             <param-name>contextConfigLocationparam-name>
32             <param-value>cn.coreqi.config.WebConfigparam-value>
33         init-param>
34         
35         <load-on-startup>1load-on-startup>
36     servlet>
37     
38     <servlet-mapping>
39         <servlet-name>dispatcherServletservlet-name>
40         <url-pattern>/url-pattern>
41     servlet-mapping>
42 web-app>

(3)根配置类(初始化项目,所以没啥内容)

1 package cn.coreqi.config;
2 
3 import org.springframework.context.annotation.Configuration;
4 
5 @Configuration
6 public class RootConfig {
7 }

(4)DispatcherServlet配置类

 1 package cn.coreqi.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.*;
 8 import org.thymeleaf.TemplateEngine;
 9 import org.thymeleaf.spring4.SpringTemplateEngine;
10 import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
11 import org.thymeleaf.spring4.view.ThymeleafViewResolver;
12 import org.thymeleaf.templatemode.TemplateMode;
13 import org.thymeleaf.templateresolver.ITemplateResolver;
14 
15 @Configuration
16 @EnableWebMvc   //启用Spring MVC 注解驱动  
17 @ComponentScan("cn.coreqi.controller")
18 public class WebConfig extends WebMvcConfigurerAdapter {
19 
20     //配置Thymeleaf视图解析器
21     @Bean
22     public ViewResolver viewResolver(TemplateEngine templateEngine){
23         ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
24         viewResolver.setTemplateEngine(templateEngine);
25         viewResolver.setCharacterEncoding("utf-8");
26         return viewResolver;
27     }
28 
29     //创建模板引擎
30     @Bean
31     public TemplateEngine templateEngine(ITemplateResolver templateResolver){
32         SpringTemplateEngine templateEngine = new SpringTemplateEngine();
33         templateEngine.setTemplateResolver(templateResolver);
34         return templateEngine;
35     }
36 
37     //创建Thymeleaf模板解析器
38     @Bean
39     public ITemplateResolver templateResolver(){
40         SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
41         resolver.setPrefix("/WEB-INF/templates/");
42         resolver.setSuffix(".html");
43         resolver.setTemplateMode(TemplateMode.HTML);
44         resolver.setCacheable(false);
45         resolver.setCharacterEncoding("utf-8");
46         return resolver;
47     }
48 
49     //配置对静态资源的处理
50     @Override
51     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
52         configurer.enable();
53     }
54 
55 }

 

你可能感兴趣的:(基于XML搭建SpringMVC项目)