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>

目录
相关文章
springMvc45-自定义配置类
springMvc45-自定义配置类
88 0
springboot自定义外部扩展文件
在springboot启动的时候调用run方法,可以看到run方法里面的内容,其中有一个getRunListeners方法
|
XML 前端开发 Java
springmvc基础使用配置
前言       本案例是在idea编辑器下,maven管理项目的前提下。 步骤      1.新建maven项目           2.配置web.xml    springDispatcherServlet org.
1178 0
|
应用服务中间件
servlet中用注解的方式读取web.xml中的配置信息
  在学习servletContext的时候,我们知道了可以在web.xml中通过标签来定义全局的属性(所有servlet都能读取的信息),并在servlet中通过servletContext.getInitParameter("name")的方式读取,java5以后提供了新的方案叫做资源注入就是通过注解(Annotation)的方式.
865 0
|
Java 数据库连接 测试技术
springboot写自定义参数验证方式
本次发表文章距上次发表已近有两月有余,原因是两月前离开了上家公司(离开原因可能会在年终终结叙述,本篇暂且忽略),来到了现在所在的京东集团,需要花时间熟悉环境和沉淀一下新的东西,因此写文章也暂时没那么勤奋了,不得不说这次是机遇也是对自己职业生涯的一次重要决定。
2276 0
springboot写自定义参数验证方式
|
SQL 关系型数据库 MySQL
SpringBoot自定义配置注入的方式:自定义配置文件注入,从mysql读取配置进行注入
SpringBoot自定义配置注入的方式:自定义配置文件注入,从mysql读取配置进行注入
345 0
|
前端开发 Java 数据格式
springMVC4(1)角色解析与入门配置实例
<div class="markdown_views"> <p>在一个web项目中,典型的MVC架构将后台分为Controller、Service、DAO三层,分别实现不同的逻辑功能,下面是一个web请求过程中,我们后台的处理过程:</p> <div class="sequence-diagram"><svg style="overflow: hidden; positi
2120 0
|
NoSQL Java Redis
自定义一个spring自动配置类
自定义一个spring自动配置类
206 0

热门文章

最新文章