SpringMVC Web配置启动

本文介绍了SpringMVC的Web配置启动过程,包括ContextLoader的初始化,ContextLoaderListener的创建,DispatcherServlet的配置以及load-on-startup参数设置。此外,还提到了Servlet的上下文参数contextClass和contextConfigLocation,并指出DispatcherServlet作为前端控制器的重要角色。最后,讨论了Web.xml中Filter的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 SpringMVC Web配置启动

有两种方式,ContextLoaderListener和ContextLoaderPlugIn

 首先配置 

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

@API ContextLoader

Performs the actual initialization work for the root application context. Called by ContextLoaderListener.

Looks for a "contextClass" parameter at the web.xml context-param level to specify the context class type, falling back to the default of XmlWebApplicationContext if not found.


@API XmlWebApplicationContext

WebApplicationContext implementation which takes its configuration from XML documents, understood by an XmlBeanDefinitionReader. This is essentially the equivalent of AbstractXmlApplicationContext for a web environment.这里先不管

By default, the configuration will be taken from "/WEB-INF/applicationContext.xml" for the root context, and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet" (like for a DispatcherServlet instance with the servlet-name "test").

For a WebApplicationContext that reads in a different bean definition format, create an analogous subclass of AbstractRefreshableWebApplicationContext. Such a context implementation can be specified as "contextClass" context-param for ContextLoader or "contextClass" init-param for FrameworkServlet.

接下来回归到这里

@API ContextLoaderListener()

Create a new ContextLoaderListener that will create a webapplication context based on the "contextClass" and"contextConfigLocation" servlet context-params.

接下来,配置servlet的上下文参数contextClass和contextConfigLocation 下图仅供参考

<servlet>

   <servlet-name>springMVC</servlet-name>

   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath*:config/springMVC-servlet.xml</param-value>

</init-param>

   <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
我们使用spring提供的DispatcherServlet


@API DispatcherServlet


public class DispatcherServlet  extends FrameworkServlet
Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters. Dispatches to registered handlers for processing a web request, providing convenient mapping and exception handling facilities.

 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。

load-on-startup表示启动容器时初始化该Servlet;

然后,我们引入了servlet,需要对servlet进行应用性的配置。我们可以配置一个

springMVC-servlet.xml
文件,如下:

     <!-- 注解扫描包 -->
    <context:component-scan base-package="org.foreveross.web.controller" />
    <!-- 开启注解 -->
    
    <mvc:annotation-driven/>
    
    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
    <!-- 静态资源访问 -->
     <mvc:resources location="/img/" mapping="/img/**"/>  
     <mvc:resources location="/js/" mapping="/js/**"/>   
     <mvc:resources location="/css/" mapping="/css/**"/>  


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <property name="defaultEncoding" value="utf-8" />
          <property name="maxUploadSize" value="10485760000" />
          <property name="maxInMemorySize" value="40960" />
    </bean>

回到Web.xml文件,我们还需要配置 Filter标签。Filter可以在@API org.springframework.web.filter中找到

如下:

<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>

<filter>
      <filter-name>openSession</filter-name>
      <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
  
<filter-mapping>
     <filter-name>openSession</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值