SpringMVC几种创建方式以及配置

简介: SpringMVC几种创建方式以及配置

1、如何在IntelliJ IDEA中将Spring MVC框架的Web应用程序部署到应用服务器

 此博客讲解JavaConfig方式的配置(零配置,全注解的方式)和web.xml的配置两种方式。零配置如果没有生效,可尝试在当前项目新建resources/META-INF/services下新建javax.servlet.ServletContainerInitializer文件,其内容为:

org.springframework.web.SpringServletContainerInitializer

为什么spirngweb中的不生效呢?有人知道吗?

2、零配置简单搭建SpringMVC 项目

3、springboot的Interceptor、Filter、Listener及注册

4、SpringBoot——》WebMvcConfigurerAdapter详解

附:

(1)web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Archetype Created Web Application</display-name>
 
    <!-- 在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?
 下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter
  这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。
   其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!-- 项目中使用Spring 时,applicationContext.xml配置文件中并没有BeanFactory,要想在业务层中的class 文件中直接引用Spring容器管理的bean可通过以下方式-->
    <!--1、在web.xml配置监听器ContextLoaderListener-->
    <!--ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
    在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。
    它的API说明
    第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。
    如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口
    第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
    BeanFactory这样一来spring中的所有bean都由这个类来创建
     IUploaddatafileManager uploadmanager = (IUploaddatafileManager)    ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");
     -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <!--2、部署applicationContext的xml文件-->
    <!--如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,
    在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。
    如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:
    在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔。
    也可以这样applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,
    applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。
    在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
 
    <servlet>
        <servlet-name>spring_mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--<init-param>-->
        <!--<param-name>contextConfigLocation</param-name>-->
        <!--其中<param-value>**.xml</param-value> 这里可以使用多种写法-->
        <!--1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml-->
        <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>-->
        <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>-->
        <!--4、多个值用逗号分隔-->
        <!--<param-value>classpath:spring/dispatcher-servlet.xml</param-value>-->
        <!--</init-param>-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring_mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2、applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="org.example.d3"/>
 
</beans>

3、dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
    <!--此文件负责整个mvc中的配置-->
 
    <!--启用spring的一些annotation -->
    <context:annotation-config/>
 
    <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
    <mvc:annotation-driven/>
 
    <!--静态资源映射-->
    <!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
    <mvc:resources mapping="/css/**" location="/static/css/"/>
    <mvc:resources mapping="/js/**" location="/static/js/"/>
    <mvc:resources mapping="/image/**" location="/static/images/"/>
    <mvc:default-servlet-handler/>  <!--这句要加上,要不然可能会访问不到静态资源,具体作用自行百度-->
 
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
    <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/><!--设置JSP文件的目录位置-->
        <property name="suffix" value=".jsp"/>
        <property name="exposeContextBeansAsAttributes" value="true"/>
    </bean>
 
    <!-- 自动扫描装配 -->
    <context:component-scan base-package="org.example.d3"/>
 
</beans>

目录
相关文章
|
9月前
|
Go API
Go技巧:用 `errgroup` 优雅管理并发任务
Go技巧:用 `errgroup` 优雅管理并发任务
|
6月前
|
Web App开发 人工智能 自然语言处理
我解放啦,网页终于能自己干活了!阿里 2 万 Star 开源 Page Agent,20 次点击变一句话
Page Agent 是阿里开源的页面内 GUI Agent:不用 Python、无头浏览器或强制浏览器插件,前端接入 JavaScript 后,就能让用户用自然语言操作网页。
1710 4
我解放啦,网页终于能自己干活了!阿里 2 万 Star 开源 Page Agent,20 次点击变一句话
|
XML 关系型数据库 MySQL
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
【MySQL异常】ExecutorException: Error getting generated key or setting result to parameter object
1319 0
|
7月前
|
存储 缓存 监控
【Redis】Redis性能优化:Pipeline、批量操作、Lua脚本、内存优化、慢日志分析
本体系构建Redis性能优化完整知识链,覆盖Pipeline(降RTT)、批量命令(提原子性)、Lua脚本(强一致+可编程)、内存优化(控碎片/精结构)及慢日志分析(根因诊断)五大模块,强调“先诊断、再优化、重闭环”,兼顾性能、稳定与可观测性。
|
6月前
|
安全 Java C++
Java 集合框架源码核心解析
Java集合框架源码核心解析,聚焦面试与实战高频点:ArrayList(动态数组、1.5倍扩容)、LinkedList(双向链表)、HashMap(数组+链表+红黑树、CAS不安全)、ConcurrentHashMap(CAS+synchronized线程安全),直击底层原理与性能本质。(239字)
393 0
|
8月前
|
存储 安全 Java
ConcurrentHashMap 深度解析:从 JDK7 到 JDK8 的演进与并发安全保障
本文详解 Java 中 `ConcurrentHashMap` 的演进:JDK7 采用分段锁(Segment)提升并发性;JDK8 重构为 CAS + synchronized + 红黑树,支持并发扩容与更优查询性能。对比差异,剖析线程安全机制与使用要点。
473 12
|
前端开发 JavaScript API
oss大文件上传
oss大文件上传
2532 6
|
Java 应用服务中间件 Maven
idea+maven+tomcat+spring 创建一个jsp项目
这篇文章介绍了如何在IntelliJ IDEA中使用Maven和Tomcat创建一个JSP项目,包括配置Maven依赖、设置Tomcat服务器、编写JSP页面、创建控制器和配置文件,以及项目的运行结果。
1417 0
idea+maven+tomcat+spring 创建一个jsp项目
|
XML Java 应用服务中间件
Tomcat项目创建 以及 在IDEA当中集成Tomcat
Tomcat项目创建 以及 在IDEA当中集成Tomcat
|
Java Linux Maven
Centos7.6安装Maven3.8.3并配置阿里云镜像
Centos7.6安装Maven3.8.3并配置阿里云镜像
2029 0

热门文章

最新文章