zoukankan      html  css  js  c++  java
  • 基于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 }
  • 相关阅读:
    MySQL锁系列3 MDL锁
    MySQL锁系列2 表锁
    MySQL锁系列1
    MySQL open table
    MySQL优化器join顺序
    MySQL优化器cost计算
    MySQL源码 优化器
    MySQL源码 解析器
    MySQL源码 数据结构hash
    微信小程序爬坑日记
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10260829.html
Copyright © 2011-2022 走看看