第一步:新建一个动态web项目,目录结构如下:
第二步,定义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"
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>SpringMvcTest</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Spring IOC 容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:WebContent/config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC 的 DispatcherServlet 控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:WebContent/config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->
<filter>
<filter-name>characterEncodingFilter</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>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 为了使用SpringMVC框架实现REST风格,需要配置 HiddenHttpMethodFilter-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
xmlns和xmlns:xsi有什么不同?
xmlns表示默认的Namespace。例如Spring XML文档中的
1
|
xmlns="http://www.springframework.org/schema/beans" |
这一句表示该文档默认的XML Namespace为http://www.springframwork.org/schema/beans。对于默认的Namespace中的元素,可以不使用前缀。例如Spring XML文档中的
1
2
3
|
< bean id = "xxx" class = "xxx.xxx.xxx.Xxx" > < property name = "xxx" value = "xxxx" /> </ bean > |
xmlns:xsi表示使用xsi作为前缀的Namespace,当然前缀xsi需要在文档中声明。
xsi:schemaLocation有何作用?
而xsi:schemaLocation,可由多个url对组成,每个url之间用空格符隔开。它怎么理解呢?举个例子:如xsi:A=“B”
“C”。假如前面已经定义过xmlns:xsi="..."。则,那句话就表示,命名空间为xsi的标签A,A的内容为B,路径为C。
所以在看到xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"这样的,也可以理解了。