web.xml配置
<!-- (1)配置前端控制器dispatcherServlet,被拦截的URL都会回到这里,然后分发到相应的Controller -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置SpringMVC配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<!-- tomcat启动后立即加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- (2)前端控制器dispatcherServlet拦截匹配的URL -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!-- 拦截所有的URL -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- (3)ServletContextListener监听器,用来加载XML配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- (4)配置文件列表,ServletContextListener会加载该参数配置的文件,如果Spring有多个XML配置文件,可以在这里配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--
classpath:spring-mvc.xml,
classpath:ApplicationContext.xml -->
</param-value>
</context-param>
<!-- (5)编码字符集统一为UTF-8(过滤器) -->
<filter>
<display-name>encodingFilter</display-name>
<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>
</filter>
dispatcherServlet配置文件spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter两个bean,
是spring MVC为@Controllers分发请求所必须的。并提供了:数据绑定支持,@NumberFormatannotation支持,
@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson) -->
<mvc:annotation-driven/>
<!-- 启动扫描 -->
<context:component-scan base-package="lzgsea" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 配置视图解析器ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>