1. 程式人生 > >基於XML搭建SpringMVC專案

基於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.springframework</groupId> 3 <artifactId>spring-webmvc</artifactId> 4 <version>4.3.21.RELEASE</version> 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 <!--指定根配置使用Java配置類--> 7 <context-param> 8 <param-name>contextClass</param-name> 9 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 10 </context-param> 11 <!--指定根配置Java配置類所在路徑--> 12 <context-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>cn.coreqi.config.RootConfig</param-value> 15 </context-param> 16 <!--註冊ContextLoaderListener--> 17 <listener> 18 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 19 </listener> 20 <!--註冊DispatcherServlet--> 21 <servlet> 22 <servlet-name>dispatcherServlet</servlet-name> 23 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 24 <!--指定DispatcherServlet配置使用Java配置類--> 25 <init-param> 26 <param-name>contextClass</param-name> 27 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 28 </init-param> 29 <!--指定DispatcherServlet配置Java配置類所在路徑--> 30 <init-param> 31 <param-name>contextConfigLocation</param-name> 32 <param-value>cn.coreqi.config.WebConfig</param-value> 33 </init-param> 34 <!--這是啟動優先順序吧--> 35 <load-on-startup>1</load-on-startup> 36 </servlet> 37 <!--DispatcherServlet對映--> 38 <servlet-mapping> 39 <servlet-name>dispatcherServlet</servlet-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 註解驅動  <mvc:annotation-driven />
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 }