SpringMVC关联配置文件的三种方式

SpringMVC关联配置文件的三种方式

八月 19, 2019

springmvc的核心servlet

第一种:[servlet-name]-servlet.xml ,比如:springmvc-servlet.xml

1.web.xml

1
2
3
4
5
6
7
8
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

2.WEB-INF文件夹下的建立springmvc-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- 配置自定义扫描包 -->
<context:component-scan base-package="com.hairui.web"></context:component-scan>

<!-- 映射物理路径 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

第二种是:改变命名空间 namespace,必须配置文件在放入在web-inf目录下。

1.web.xml

1
2
3
4
5
6
7
8
9
10
11
12
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>namespace</param-name>
<param-value>配置文件名XXXX(不带后缀)</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

2.WEB-INF文件夹下的建立配置文件XXXX.xml,文件名与web.xml中的对应

第三种是通过:contextConfigLocation

1.web.xml

1
2
3
4
5
6
7
8
9
10
11
12
<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*:配置文件名XXXX.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

2.reaourcea文件夹下的建立配置文件XXXX.xml,文件名与web.xml中的对应