有关Spring-MVC配置的详细介绍在配置文件中都已经注明,这里就直接将两个主要配置文件spring-context.xml和web.xml复制如下,自己以后也方便查看。
spring-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- <context:annotation-config /> -->
<!-- 自动扫描指定包下的所有内容 -->
<context:component-scan base-package="com.leox" />
<!-- 项目中使用注解 -->
<mvc:annotation-driven />
<!-- 主要是用来进行静态资源的访问,spring3.0.4出现的,
即所有的页面引用到/static/**的资源都从/include/**里面进行查找。
这样做也可以在实际项目中隐藏文件的真实路径 -->
<mvc:resources location="/include/**" mapping="/static/**" />
<!-- 访问项目根路径,映射到welcome页面去 -->
<mvc:view-controller path="/" view-name="hello" />
<!-- 引用其他的spring配置文件(假如项目由多个人进行开发,就可以定义多个这样的配置文件由每个人单独开发,最后只要在这引入就可以了) -->
<!-- <beans:import resource="app-context.xml" /> -->
<!-- 将/WEB-INF/pages/下的所有页面交给类InternalResourceViewResolver进行处理,
并且在访问页面后面自动加上.jsp后缀。比如访问hello.jsp时只需要访问hello即可 -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/pages/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring Mvc</display-name>
<!-- SpringMVC是一个基于DispatcherServlet的MVC框架,
每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请求给相应的Handler,
Handler处理以后再返回相应的视图(View)和模型(Model),返回的视图和模型都可以不指定,
即可以只返回Model或只返回View或都不返回。 -->
<servlet>
<!-- 这里给 servlet-name定义的名称是appServlet,
这样的话会在web-inf下spring会自动扫描一个XML文件名叫appServlet-servlet.xml文件,
这里都是spring自动扫描的
-->
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 如果這裡指定了該參數,spring就会扫描指定文件,我这里指定的是spring-context.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 映射项目的根目录下,即所有文件 -->
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符集过滤器 -->
<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>
</web-app>