将ContextLoaderListener添加到Spring MVC中的web.xml

use*_*403 18 java spring spring-mvc

我是Spring MVC的新手.我有一个Web应用程序.我有以下配置:

<welcome-file-list>
    <welcome-file>list.html</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)


我是否需要将以下行添加到web.xml文件中?

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

Jig*_*shi 26

是的,你需要添加ContextLoaderListenerweb.xml,只是如果要加载其他Spring上下文的XML文件,以及在加载应用程序,你可以为他们指定

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)


gka*_*mal 15

仅当您有两个config xml文件时.一个是Services/DAO,另一个是Controller.如果您在一个spring配置文件中配置了所有内容,则不需要ContextLoaderListener,只需调度程序servlet即可.

建议将配置拆分为两个,并使用ContextLoaderListener创建根应用程序上下文和调度程序servlet来创建Web层应用程序上下文.


小智 6

它是可选的,你真的不需要它只适用于Spring MVC(DispatcherServlet会这样做).但是必须在Spring Spring上添加Spring安全性

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

只需一句话,如果使用ContextLoaderListener你将不得不添加DelegatingFilterProxy:

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

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/admin</url-pattern>
</filter-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>     
    /WEB-INF/spring-security.xml
    </param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

在你的web.xml中也是如此.抱歉四年太晚了.干杯