gpt4 book ai didi

java - 为什么在SpringMVC中rest-servlet,xml是必须的?

转载 作者:行者123 更新时间:2023-12-04 07:22:27 25 4
gpt4 key购买 nike

在我的web.xml中,主要配置如下:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-mvc-config.xml
</param-value>
</context-param>

<filter>
<filter-name>SetCharacterEncoding</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>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

在我的spring-mvc-config.xml中,只有两行:

<mvc:annotation-driven />
<import resource="spring.xml" />

接下来,在我的 spring.xml 中,所有关于 spring 配置的东西,没有任何关于 springMVC 配置的内容。

当我在 tomcat 中启动这个 webapp 时,它总是在 [WEB-INF/rest-servlet.xml] 上抛出一个 FileNotFoundException,在我添加它之后,它只是工作正常。

我只想知道web.xml 中的哪一部分指示在WEB-INF 目录中必须有rest-servlet.xml

我用谷歌搜索过,但一无所获。谁能帮帮我?非常感谢!

最佳答案

您已将 DispatcherServlet 命名为“rest”,因此默认情况下 Spring MVC 正在寻找 rest-servlet.xml。如果您想使用不同的文件名,请执行以下操作:

<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/spring-mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

关于java - 为什么在SpringMVC中rest-servlet,xml是必须的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19555316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com