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

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

被折叠的 条评论
为什么被折叠?



