Spring:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [] 的 HTTP 请求的映射

Posted

技术标签:

【中文标题】Spring:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [] 的 HTTP 请求的映射【英文标题】:Spring: No mapping found for HTTP request with URI [] in DispatcherServlet with name 'dispatcher' 【发布时间】:2015-02-26 14:00:30 【问题描述】:

我正在尝试使用 Spring Security 实现对我的 Web 应用程序的登录。 即使用户成功登录,我也会收到上述警告消息。相同的配置适用于其他应用,但不适用于这个。请帮助

我的 web.xml 文件:

     <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/dispatcher-servlet.xml
            /WEB-INF/security-config.xml            
            /WEB-INF/mongo-config.xml
        </param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>

    <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>/*</url-pattern>
    </filter-mapping>



</web-app>

安全配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
            http://www.springframework.org/schema/security
            http://www.springframework.org/schema/security/spring-security.xsd
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- enable use-expressions -->
    <security:http auto-config="true" use-expressions="true" access-denied-page="/denied">
        <security:intercept-url pattern="/login" access="permitAll"/>
        <security:intercept-url pattern="/register" access="permitAll"/>
        <security:intercept-url pattern="/addUser" access="permitAll"/>
        <security:intercept-url pattern="/home" access="hasRole('ROLE_USER')"/>
        <security:form-login
            login-page="/login"
            authentication-failure-url="/login?error=true"
            default-target-url="/home"/>

        <security:logout
            invalidate-session="true"
            logout-success-url="/login"
            logout-url="/logoutServlet"/>
    </security:http>

    <security:authentication-manager>
         <security:authentication-provider user-service-ref="customUserDetailsService">
           <security:password-encoder ref="passwordEncoder"/>
         </security:authentication-provider>
    </security:authentication-manager>

    <!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
    <beans:bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder">
    </beans:bean>

    <!-- A custom service where Spring will retrieve users and their corresponding access levels  -->
    <beans:bean id="customUserDetailsService" class="service.CustomUserDetailsService">
    </beans:bean>

</beans:beans>

调度程序-servlet 文件

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/fonts/**" location="/fonts/" />
    <mvc:resources mapping="/javascript/**" location="/javascript/" />
    <mvc:annotation-driven />

    <context:component-scan base-package="controller" />
    <!-- <context:component-scan base-package="services" /> -->

    <context:annotation-config />
    <bean id="imapService" class="service.ImapServiceImpl"></bean>
    <bean id="userService" class="service.UserServiceImpl"></bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000" />
    </bean>
</beans>

显示警告: 警告 org.springframework.web.servlet.PageNotFound - 在 DispatcherServlet 中找不到带有 URI [/Webclient/home] 的 HTTP 请求的映射,名称为“dispatcher”

【问题讨论】:

错误提示 dispatcher-servlet.xml 中没有 /Webclient/home 的映射。您的控制器是否具有上述 URL 的请求映射?您可以发布具有该映射的控制器吗? 谢谢小黄人。我没有在我的控制器中映射 /home @RequestMapping(value = "/home") public String gethome() return "home"; 写了这个。现在它工作正常。 你能接受它作为答案吗? 【参考方案1】:

控制器似乎没有 /home 的映射,这就是它失败的原因。请添加并尝试一下。

【讨论】:

以上是关于Spring:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [] 的 HTTP 请求的映射的主要内容,如果未能解决你的问题,请参考以下文章

javax.servlet.ServletException:无法在名称为“NA-dispatcher”的 servlet 中解析名称为“login”的视图

在 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml] 中定义名称为“entityManagerFactory”的 bean 创建错误:

Spring MVC 无法正确配置 Dispatcher Servlet

Spring Integration Dispatcher 没有频道订阅者

Spring 中的 Dispatcher Servlet 是啥?

Dispatcher 没有频道订阅者 - spring-cloud-stream-kafka