基于XML搭建SpringMVC项目

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

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

1.pom中添加SpringMVC相关jar

     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>4.3.21.RELEASE</version>
     </dependency>

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

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

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://java.sun.com/xml/ns/javaee"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          id="WebApp_ID" version="2.5">
     <!--指定根配置使用Java配置类-->
     <context-param>
         <param-name>contextClass</param-name>
         <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
     </context-param>
     <!--指定根配置Java配置类所在路径-->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>cn.coreqi.config.RootConfig</param-value>
     </context-param>
     <!--注册ContextLoaderListener-->
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <!--注册DispatcherServlet-->
     <servlet>
         <servlet-name>dispatcherServlet</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <!--指定DispatcherServlet配置使用Java配置类-->
         <init-param>
             <param-name>contextClass</param-name>
             <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
         </init-param>
         <!--指定DispatcherServlet配置Java配置类所在路径-->
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>cn.coreqi.config.WebConfig</param-value>
         </init-param>
         <!--这是启动优先级吧-->
         <load-on-startup>1</load-on-startup>
     </servlet>
     <!--DispatcherServlet映射-->
     <servlet-mapping>
         <servlet-name>dispatcherServlet</servlet-name>
         <url-pattern>/</url-pattern>
     </servlet-mapping>
 </web-app>

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

package cn.coreqi.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class RootConfig {

}

4.DispatcherServlet配置类

package cn.coreqi.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.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

@Configuration
@EnableWebMvc   //启用Spring MVC 注解驱动  <mvc:annotation-driven />
@ComponentScan("cn.coreqi")
public class WebConfig extends WebMvcConfigurerAdapter {

 //配置Thymeleaf视图解析器
 @Bean
 public ViewResolver viewResolver(TemplateEngine templateEngine){
	 ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
	 viewResolver.setTemplateEngine(templateEngine);
	 viewResolver.setCharacterEncoding("utf-8");
	 return viewResolver;
 }

 //创建模板引擎
 @Bean
 public TemplateEngine templateEngine(ITemplateResolver templateResolver){
	 SpringTemplateEngine templateEngine = new SpringTemplateEngine();
	 templateEngine.setTemplateResolver(templateResolver);
	 return templateEngine;
 }

 //创建Thymeleaf模板解析器
 @Bean
 public ITemplateResolver templateResolver(){
	 SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
	 resolver.setPrefix("/WEB-INF/templates/");
	 resolver.setSuffix(".html");
	 resolver.setTemplateMode(TemplateMode.HTML);
	 resolver.setCacheable(false);
	 resolver.setCharacterEncoding("utf-8");
	 return resolver;
 }

 //配置对静态资源的处理
 @Override
 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
	 configurer.enable();
 }
}
posted @ 2019-01-12 19:53  SpringCore  阅读(483)  评论(0编辑  收藏  举报