org.springframework.web.servlet.DispatcherServlet noHandlerFound

org.springframework.web.servlet.DispatcherServlet noHandlerFound

No mapping found for HTTP request with URI [/] in DispatcherServlet with name


在配置SpringMVC环境时,常常出现访问的URL出错,报404错误,如果URL确定正确的话,通常都是我们配置错误

出现这个问题,毫无疑问,是我们配置错了

主要的错误处在两处,第一就是web.xml,完整的代码如下,仅作参考

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3.0.xsd"
    metadata-complete="true" version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
        
    <servlet>
        <servlet-name>Workflow</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:Workflow-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Workflow</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>


需要注意上面的*.do 您也可以更改为 /

第二个问题,也是比较奇怪的问题,像我上面配置,就需要在Workflow-servlet.xml 添加一个静态资源,如果没有

    <!-- 静态资源不过滤直接访问,否则图片等资源无法访问 web.xml,已配置拦截 .do 所以这里可不需要,如果添加任然有效 --> 
    <mvc:resources location="/WEB-INF/resource/" mapping="res/**/**.**" />

自此,问题解决


如果您觉得本文章对您有用,也为了更好的运营博客,您可以点击这里捐助

评论 抢沙发

昵称 (必填) 邮箱 (必填)
表情