MVC配置文件及位置

配置文件位置

默认位置

WEB-INF目录下,文件名:<servlet-name>-servlet.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

指定位置

在web.xml中配置

DispatcherServlet中的contextConfigLocation属性可以指定配置文件位置及文件名

确保配置文件存在于类路径(Resources)下

web.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <!--配置前端控制器-->
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 手动设置 Spring MVC 配置文件的路径 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!-- 服务器启动时初始化 Spring MVC 控制器 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

SpringMVC容器文件

XML 复制代码
<?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"
       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.2.xsd">
    <!-- 配置组件扫描 -->
    <context:component-scan base-package="com.mvc" />

    <!--配置视图解析器[默认视图解析器]-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置属性suffix 和 prefix-->
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
        <!--调整优先级-->
        <property name="order" value="10"/>
    </bean>
</beans>
相关推荐
optimistic_chen1 天前
【Java EE进阶 --- SpringBoot】初识Spring(创建SpringBoot项目)
spring boot·后端·spring·java-ee·tomcat·mvc·idea
不知名搬运工3 天前
9 ABP Framework 中的 MVC 和 Razor Pages
mvc
周杰伦fans5 天前
.NET Core MVC中CSHTML
mvc·.netcore
右手嘚温暖6 天前
SpringMvc的原理深度剖析及源码解读
spring·开源·mvc
Alt.97 天前
SpringMVC(二)
java·开发语言·mvc
ysy16480672399 天前
Spring、Spring MVC、MyBatis 和 Spring Boot的关系
spring·mvc·mybatis
探索java10 天前
Spring MVC框架中全局异常处理机制详解
java·spring·mvc
可曾去过倒悬山11 天前
SpringMvc跨域配置方法详解
spring·mvc
Alt.911 天前
SpringMVC(一)
java·mvc
杨DaB12 天前
【项目实践】在系统接入天气api,根据当前天气提醒,做好plan
java·后端·spring·ajax·json·mvc