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

*搭建SpringMVC需要在web.xml中注册DispatcherServlet和ContextLoaderListener,同时他们两个之间会分别通过上下文参数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应用上下文,我们需要配置DispatcherServlet和ContextLoaderListener的contextClass上下文参数为AnnotationConfigWebApplicationContext,AnnotationConfigWebApplicationContext是WebApplicationContext的实现类,它会加载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.controller")
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();
} }

基于XML搭建SpringMVC项目的更多相关文章

  1. 基于XML搭建Dubbo项目

    (1).新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等. 公共Bean: package cn.coreqi.entities; import java.io.Serializ ...

  2. 基于Spring注解搭建SpringMVC项目

    在2018寒冬,我下岗了,因为我的左脚先迈进了公司的大门.这不是重点,重点是我扑到了老板小姨子的怀里. 网上好多教程都是基于XML的SpringMVC,想找一篇注解的,但是写的很模糊,我刚好学到这里, ...

  3. 零配置简单搭建SpringMVC 项目

    SpringMVC是比较常用的JavaWeb框架,非常轻便强悍,能简化Web开发,大大提高开发效率,在各种Web程序中广泛应用.本文采用Java Config的方式搭建SpringMVC项目,并对Sp ...

  4. 工具idea 基于maven 创建springMVC项目

    SpringMVC Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制器一般不 ...

  5. 基于docker搭建laravel项目

    基于docker搭建laravel项目 公司PHP项目是Laravel框架写的,目前环境需要通过docker来部署一下.网上学习了一下相关知识.整理后做一个笔记.用到定时任务crontab与进程管理s ...

  6. maven -- 学习笔记(四)实现在Eclipse用maven搭建springmvc项目(附构建步骤和详细实现代码)

    Learn from:http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html,感谢楼主的分享,才有下面的这篇学习小结 一.环境准 ...

  7. 项目搭建系列之一:使用Maven搭建SpringMVC项目

    约定电脑都安装了eclipse,且已配置好Maven以及eclipse插件. 1.Eclipse 2.maven 3.Eclipse 需要安装maven插件.url:maven - http://do ...

  8. 从头开始基于Maven搭建SpringMVC+Mybatis项目(1)

    技术发展日新月异,许多曾经拥有霸主地位的流行技术短短几年间已被新兴技术所取代. 在Java的世界中,框架之争可能比语言本身的改变更让人关注.近几年,SpringMVC凭借简单轻便.开发效率高.与spr ...

  9. 从头开始基于Maven搭建SpringMVC+Mybatis项目(3)

    接上文内容,本节介绍基于Mybatis的查询和分页功能,并展示一个自定义的分页标签,可重复使用以简化JSP页面的开发. 从头阅读传送门 在上一节中,我们已经使用Maven搭建好了项目的基础结构,包括一 ...

随机推荐

  1. 查看linux的公网地址

    在搭建环境时,我们需要搭建公网IP地址,因此我们可以使用以下命令查看公网IP地址 curl ifconfig.me 同时我们可以通过以下命令查看公网地址 curl cip.cc

  2. [代码]解析nodejs的require,吃豆人的故事

    最近在项目中需要对nodejs的require关键字做解析,并且替换require里的路径.一开始我希望nodejs既然作为脚本语言,内核提供一个官方的parser库应该是一个稳定可靠又灵活的渠道,然 ...

  3. 工具(2): 极简MarkDown排版介绍(How to)

    如何切换编辑器 切换博客园编辑器为MarkDown:MarkDown Editor 选择一个在线编辑和预览站点:StackEdit 如何排版章节 MarkDown: 大标题 ========== 小标 ...

  4. [蓝桥杯]2015蓝桥省赛B组题目及详解

    /*——————————————————————————————————————————————————————————— [结果填空题]T1 题目:奖券数目 有些人很迷信数字,比如带“4”的数字,认 ...

  5. Get Remote Computer Install Software

    #requires -Version 2 function Get-Software { param ( [string] $DisplayName='*', [string] $UninstallS ...

  6. django rest framework serializers

    django rest framework serializers序列化   serializers是将复杂的数据结构变成json或者xml这个格式的 serializers有以下几个作用:- 将qu ...

  7. gogs git代码管理

    Gogs 是一个基于 Go语言的开源的 Git 服务端.非常轻量,安装也很简单.官网https://gogs.io/docs/installation/install_from_binary 下载后解 ...

  8. 【THUSC2017】【LOJ2978】杜老师 高斯消元

    题目大意 给你 \(l,r\),求从 \(l\) 到 \(r\) 这 \(r-l+1\) 个数中能选出多少个不同的子集,满足子集中所有的数的乘积是一个完全平方数. 对 \(998244353\) 取模 ...

  9. Python_002_Python语言基础

    ♥2.1  Python的程序概述 Python程序可以分解为模块.语句.表达式.对象 ♥2.2  Python对象和引用 2.2.1  Python对象概述 对象:标识(identity).类型(t ...

  10. Linux命令_sed_2

    2.替换(将包含"xxx"的行中的"yyy"替换成"zzz") 现有文件“replace_specified_contained_line” ...