srping MVC 工程简单搭建

Spring版本:3.2.2.RELEASE

第一步:导入必须的jar包

spring-beans.jar

spring-context.jar

spring-core.jar

spring-expression.jar

spring-web.jar

spring-webmvc.jar

commons-fileupload-1.2.2.jar

commons-logging-1.1.1.jar

第二步:在web.xml配置DispatcherServlet

DispatcherServlet实际上是一个Servlet(它从HttpServlet继承而来)。和其它Servlet一样,DispatcherServlet定义在web应用的web.xml文件里。DispatcherServlet处理的请求必须在同一个web.xml文件里使用url-mapping定义映射。

Spring的DispatcherServlet有一组特殊的bean,用来处理请求和渲染相应的视图。这些bean包含在Spring的框架里,可以在WebApplicationContext中配置,配置方式与配置其它bean相同。

WebApplicationContext中特殊的bean

名称

描述

控制器(Controller)

控制器 实现的是MVC中C 那个组成部分。

处理器映射(Handler mapping)

处理器映射包含预处理器(pre-processor),后处理器(post-processor)和控制器的列表,它们在符合某种条件时才被执行(例如符合控制器指定的URL)。

视图解析器(View resolvers)

视图解析器 可以将视图名解析为对应的视图。

本地化解析器(Locale resolver)

本地化解析器能够解析用户正在使用的本地化设置,以提供国际化视图。

主题解析器(Theme resolver)

主题解析器能够解析你的web应用所使用的主题,以提供个性化的布局。

上传文件解析器(multipart file resolver)

上传文件解析器提供HTML表单文件上传功能。

处理器异常解析器(Handler exception resolver(s))

处理器异常解析器可以将异常对应到视图,或者实现更加复杂的异常处理代码。

DispatcherServlet默认使用WebApplicationContext作为上下文,Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml,也可以自定义配置文件位置和名称。

1.使用默认配置:

<servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

2.自定义配置文件位置和名称

<servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[servlet-name]-servlet.xml-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

 3.使用监听器加载上下文配置

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- or use the ContextLoaderServlet instead of the above listener
<servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
-->

第三步:加载业务相关上下文配置

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

ApplicationContext能以声明的方式创建,如使用ContextLoader。ContextLoader接口有两个实现:ContextLoaderListener和ContextLoaderServlet。两者都实现同样的功能,但不同的是,ContextLoaderListener不能在与Servlet 2.2兼容的web容器中使用。根据Servlet 2.4规范, servlet context listener要在web应用程序的servlet context建立后立即执行,并要能够响应第一个请求(在servlet context要关闭时也一样):这样一个servlet context listener是初始化Spring ApplicationContext的理想场所。虽然使用哪个完全取决于你,但是在同等条件下应该首选ContextLoaderListener;对于更多兼容性的信息,请查看ContextLoaderServlet的JavaDoc。

原文地址:https://www.cnblogs.com/nami/p/3245380.html