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>
相关推荐
xiaolyuh1239 小时前
Spring MVC Bean 参数校验 @Validated
java·spring·mvc
heartbeat..2 天前
Spring MVC 全面详解(Java 主流 Web 开发框架)
java·网络·spring·mvc·web
自在极意功。2 天前
简单介绍SpringMVC
java·mvc·springmvc·三层架构
威桑3 天前
深入理解 MVC 模式的优缺点
系统架构·mvc
alonewolf_993 天前
Spring MVC启动与请求处理全流程解析:从DispatcherServlet到HandlerAdapter
java·spring·mvc
廋到被风吹走3 天前
【Spring】Spring MVC核心原理与RESTful最佳实践详解
spring·mvc·restful
alonewolf_994 天前
Spring MVC重点功能底层源码深度解析
java·spring·mvc
撩得Android一次心动5 天前
Android 架构模式的演变(MVC、MVP、MVVM、MVI)
android·架构·mvc·mvvm·mvp
小信丶6 天前
Spring MVC 配置器:WebMvcConfigurer 详解、应用场景和示例代码
java·spring·mvc
柒.梧.7 天前
深度解析SpringMVC实战项目:从配置到请求处理全流程
java·spring·mvc