Spring 4 MVC,警告:在 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射,名称为 [...] 和 HTTP 404 错误

Posted

技术标签:

【中文标题】Spring 4 MVC,警告:在 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射,名称为 [...] 和 HTTP 404 错误【英文标题】:Spring 4 MVC , WARNING: No mapping found for HTTP request with URI in DispatcherServlet with name [...] & HTTP 404 Error 【发布时间】:2014-08-03 19:17:44 【问题描述】:

我的项目突然崩溃了,dispatcher servlet 无法转发到我的视图,我开始初步获取

警告:在 Disp 中找不到带有 URI [/thedallasapp_poc/] 的 HTTP 请求的映射 atcherServlet 名称为 'mvc_dispatcher' & 浏览器中出现 404 错误。

在更改设置后,我在日志中没有收到 error.warning,但我收到了 404 错误。

这是我的项目设置:

Java: 1.6

网络框架:

• Spring 4.0.0.RELEASE(MVC 和其他包) • Spring 安全 3.2.4.RELEASE

ORM:

• 休眠 4.1.5.Final 清理、构建、编译、打包: • Maven

数据库:

• PostgreSQL

网络服务器: Apache Tomcat 7.0

这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hitachi</groupId>
    <artifactId>thedallasapp_poc_test</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>thedallasapp_poc_test</name>
    <url>http://maven.apache.org</url>


    <!-- properties -->
    <properties>
        <app.name>thedallassapp_poc</app.name>
        <tomcat.version>6.0.26</tomcat.version>
        <spring.version>4.0.0.RELEASE</spring.version>
        <hibernate.version>4.1.5.Final</hibernate.version>
        <spring.security.version>3.2.4.RELEASE</spring.security.version>
    </properties>

    <dependencies>

        <!-- jee -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
        </dependency>

        <!-- dependencies spring 4 -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>$spring.security.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>$spring.security.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>$spring.security.version</version>
        </dependency>


        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>


        <!-- Spring 4 dependencies END -->

        <!-- jdbc & database START -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>
        <!-- connection pool -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-dbcp</artifactId>
            <version>7.0.52</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>


        <!-- jdbc & database END -->

        <!-- hibernate START -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>$hibernate.version</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.1-GA</version>
        </dependency>

        <!-- hibernate END -->

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.1</version>

        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>

            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>c</artifactId>
            <version>1.1.1</version>

            <type>tld</type>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>fmt</artifactId>
            <version>1.1.1</version>

            <type>tld</type>
        </dependency>

        <!-- webapp-runner -->
        <dependency>
            <groupId>com.github.jsimone</groupId>
            <artifactId>webapp-runner</artifactId>
            <version>7.0.40.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <!-- build -->
    <build>
        <finalName>thedallasapp_poc</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <warFile>target/thedallasapp_poc.war</warFile>
                    <url>http://localhost:8080/manager/text</url>
                    <update>true</update>
                    <server>MyTomcat</server>
                    <path>/thedallasapp_poc</path>
                    <username>tomcat</username>
                    <password>password6678</password>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <!-- <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> -->
                    <packagingExcludes>WEB-INF/lib/tomcat*</packagingExcludes>
                    <source>1.6</source>
                    <target>1.6</target>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>com.github.jsimone</groupId>
                                        <artifactId>webapp-runner</artifactId>
                                        <version>7.0.40.0</version>
                                        <destFileName>webapp-runner.jar</destFileName>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>Toyota Relocation App</display-name>

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

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

    <!-- load spring security -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc_dispatcher-servlet.xml,/WEB-INF/security.xml</param-value>
    </context-param>

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

    <!-- spring security -->
    <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>

Eclipse 项目结构:

mvc-dispatcher-servlet:

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


    <mvc:annotation-driven />
    <!-- scan at "com.hitachi.thedallasapp_poc" for spring annotated components 
        & register in spring container -->
    <context:annotation-config />

    <!-- <mvc:default-servlet-handler/>-->

    <context:component-scan base-package="com.hitachi.thedallasapp_poc.Category" />
    <context:component-scan base-package="com.hitachi.thedallasapp_poc.location" />
    <context:component-scan base-package="com.hitachi.thedallasapp_poc.messaging" />
    <context:component-scan base-package="com.hitachi.thedallasapp_poc.userprofile" />
    <context:component-scan base-package="com.hitachi.thedallasapp_poc.test" />

    <!-- the mvc resources tag that handles static content requests under resources 
        path -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- ViewResolver -->

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

    <!-- datasource & hibernate configs -->

    <bean id="myProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath*:db-dev.properties</value>
            </list>
        </property>
    </bean>

    <!-- local datasource -->

    <bean id="postgresqlDataSource" destroy-method="close"
        class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url"
            value="jdbc:postgresql://localhost:5432/postgres?createDatabaseIfNotExist=true" />
        <property name="username" value="postgres" />
        <property name="password" value="heroku6678" />
    </bean>

    <!-- hibernate sessionFactoty via properties -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource" ref="postgresqlDataSource" />

            <!-- annotated classes -->
        <property name="annotatedClasses">
            <list>
                <value>com.hitachi.thedallasapp_poc.userprofile.UserProfile</value>
                <value>com.hitachi.thedallasapp_poc.userprofile.UserConnection</value>
                <value>com.hitachi.thedallasapp_poc.Category.Categories</value>
                <value>com.hitachi.thedallasapp_poc.location.Location</value>
                <value>com.hitachi.thedallasapp_poc.location.FavoriteLocation</value>
                <value>com.hitachi.thedallasapp_poc.messaging.Messages</value>
            </list>
        </property>
         <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 
            </props>
        </property>

    </bean>

    <!-- transaction management -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="postgresqlDataSource" />
    </bean>


    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- spring security should ignore "/" & "/resources/**" paths -->
    <http pattern="/" security="none" auto-config="true"
        use-expressions="true" />
    <http pattern="/login" security="none" auto-config="true"
        use-expressions="true" />
    <http pattern="/resources/**" security="none" auto-config="true"
        use-expressions="true" />

    <!-- custom bean used to override spring security page redirection after 
        successful authentication -->
    <beans:bean id="myAuthSuccessHandler"
        class="com.hitachi.thedallasapp_poc.security.MyAuthSuccessHandler" />


    <!-- spring security configuration intercept-url & login form -->
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/auth/**" access="hasRole('ROLE_USER')" />

        <form-login login-page="/login" authentication-failure-url="/login?error"
            username-parameter="emailInputField" password-parameter="pinInputField"
            default-target-url="/auth/welcome" always-use-default-target="true"
            authentication-success-handler-ref="myAuthSuccessHandler" />
        <!-- keep logged in session for 3 days or 259200 secs -->
        <remember-me key="uniqueAndSecret1984"
            token-validity-seconds="60" />

        <logout logout-url="/logout" logout-success-url="/login?logout=1"
            delete-cookies="JSESSIONID" invalidate-session="true" />

    </http>

    <!-- user accounts -->
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="naim@test.com" password="12345" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

类 HomeController:

package com.hitachi.thedallasapp_poc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeControler 

    public HomeControler() 
        super();
        // TODO Auto-generated constructor stub
    

    /* serves the login landing/home page view */
    @RequestMapping(value="/")
    public ModelAndView serveLanding(HttpSession session) 



        ModelAndView model = new ModelAndView();
        model.setViewName("login");
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        //check if isRememberMeAuthenticated cookie is true from previous login 
        System.out.println("Class HomeControler, String serveLanding(), isRememberMeAuthenticated():"+isRememberMeAuthenticated());
        if (isRememberMeAuthenticated()!=false && auth.isAuthenticated()!=false)    
        
            session.setAttribute("isRememberMeAuthenticated", true);

        

        return model;
    

    /* process login request from login view */
    @RequestMapping(value = "/login**")
    public ModelAndView processLogin(String error,@RequestParam(value = "logout", required = false) String logout)
    
        System.out.println("Class HomeControler,  processLogin()");
        ModelAndView model = new ModelAndView();

         //non test code

        if (error != null) 
            System.out.println("Class HomeControler,  processLogin(), error ");
            model.addObject("error", "Invalid username and password!");

        
            if (logout != null) 
                System.out.println("Class HomeControler,  processLogin(), logout ");
                model.addObject("msg", "You've been logged out successfully.");

            

            model.setViewName("login");


            return model;

    


    /* serves the login landing/home page view */
    @RequestMapping(value="/auth/welcome**")
    public ModelAndView welcome(HttpSession session) 

        System.out.println("Class HomeControler, String welcome()");

        ModelAndView model = new ModelAndView();

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        //after authentication check isRememberMeAuthenticated cookie is true
        System.out.println("Class HomeControler, String welcome(), isRememberMeAuthenticated():"+isRememberMeAuthenticated());

               if (isRememberMeAuthenticated()!=false && auth.isAuthenticated()!=false) 
                

                    session.setAttribute("isRememberMeAuthenticated", true);

                
        //get logged in username

        String name = auth.getName();
        model.addObject("loggedInUserName", name);
        model.setViewName("/auth/appView");

        return model;
    


    /* serves the login landing/home page view */
    @RequestMapping(value="/logout")
    public  ModelAndView logoutTheUserAndInvalidateSession(HttpSession session, HttpServletRequest request, HttpServletResponse response) 

        ModelAndView model = new ModelAndView();
        //logout via spring security 
         Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
            SecurityContextLogoutHandler ctxLogOut = new SecurityContextLogoutHandler();
                ctxLogOut.logout(request, response, auth); 
            //invalidate session just to make sure 
                session = request.getSession(false);
                if (session != null) 
                    session.invalidate();
                

                model.addObject("logout","1");
                model.setViewName("login");

    return model;
    


    /**
     * Check if user is login by remember me cookie, refer
     * org.springframework.security.authentication.AuthenticationTrustResolverImpl
     */
    private boolean isRememberMeAuthenticated() 

        Authentication authentication = 
                    SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) 
            return false;
        

        return RememberMeAuthenticationToken.class.isAssignableFrom(authentication.getClass());
    

    //end class HomeControler

登录查看:

<!DOCTYPE html>
<html>
<head>
<title>Toyota Relocation App</title>
<meta name="viewport"
    content="width=device-width, initial-scale=1, user-scalable=no">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec"
    uri="http://www.springframework.org/security/tags"%>
<!-- jquery styles & scripts -->
<script
    src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).bind("mobileinit", function() 
        $.mobile.ajaxEnabled = false;
    );
</script>
<script>
    $(document).ready(function() 

        // if error has data then make div visible
        <c:if test="$not empty error">
        if ($('.error_div').length > 0) 
            console.log("found .error_div");
            $('.error_div').show();
        
        </c:if>
        // if msg has data then make div visible
        <c:if test="$not empty msg">
        if ($('.msg_div').length > 0) 
            console.log("found .msg_div");
            $('.msg_div').show();
        
        </c:if>
    );
</script>
<link rel="stylesheet"
    href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.css" />
<script
    src="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.js"></script>
<!--  http://fortawesome.com -->
<link
    href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"
    rel="stylesheet">
<!-- custom stylesd & scripts -->
<link rel="stylesheet" href="resources/css/home.css" />
<script type="text/javascript" src="resources/js/home.js"></script>
</head>
<body>
    <!--  
<sec:authorize access="hasRole('ROLE_USER')">
<script>
console.log("hasRole('ROLE_USER')");
$.post( "./auth/app" );
</script>
</sec:authorize>
-->
    <div class="wrapper">

        <!-- 
        <div data-role="header" data-position="fixed">
            <div class="myheader">HEADER</div>
        </div>
        -->

        <div data-role="content">

            <div class="mycontent">
                <div id="logo_div">
                    <img src="resources/images/01_toyota_logo_small.png">
                </div>
                <div class="login_form_wrapper">
                    <!--  login error & msg jstl -->
                    <div class="error_div" style="display: none;">
                        <p class="error_text">$error</p>
                    </div>
                    <div class="msg_div" style="display: none;">
                        <p class="msg_text">$msg</p>
                    </div>
                    <!-- spring security form -->
                    <form name='loginForm'
                        action="<c:url value='/j_spring_security_check' />" method='POST'>

                        <ul class="mylistview" data-role="listview">
                            <li>
                                <div>
                                    <img id="email_input_icon"
                                        src="resources/images/01_login_email_icon_small.png">
                                </div>
                                <div id="email_input">
                                    <input name="emailInputField" " id="email_input_field"
                                        type="email" data-role="none" />
                                </div>
                            </li>
                            <li>
                                <div>
                                    <img id="pin_input_icon"
                                        src="resources/images/01_login_password_icon_small.png">
                                </div>
                                <div id="pin_input">
                                    <input name="pinInputField" id="pin_input_field"
                                        type="password" data-role="none" />
                                </div>
                            </li>
                            <li>

                                <div id="login_button">
                                    <button id="login" class="button" type="submit">LOGIN</button>
                                </div>
                                <div id="remember">
                                    <label class="checkboxLabel" data-role="none"
                                        style="float: left">
                                        <p>Remember Me</p>
                                    </label> <input data-role="none" type='checkbox'
                                        name='_spring_security_remember_me'
                                        id="_spring_security_remember_me" value="true" />
                                </div>
                            </li>
                        </ul>

                    </form>
                </div>

                <div class="or_divider">
                    <img src="resources/images/01_login_or_option_small.png">
                </div>

                <div class="options_menu">

                    <ul class="mylistview2" data-role="listview">
                        <li>
                            <div id="login_account_no_words_icon">
                                <img src="resources/images/01_login_account_no_words_small.png">
                            </div>
                            <div id="login_guest_no_words_icon">
                                <img src="resources/images/01_login_guest_no_words_small.png">
                            </div>
                        </li>
                        <li>
                            <div id="create_an_account">
                                <P>Create An Account</P>
                            </div>
                            <div id="continue_as_guest">
                                <P>Continue As Guest</P>
                            </div>
                        </li>

                    </ul>
                </div>
            </div>

        </div>
        <!-- 
        <div data-role="footer">
            <div class="myfooter">FOOTER</div>
        </div>
        -->
    </div>
</body>

</html>

通过 Eclipse 部署:

我通过 Eclipse 部署到 Tomcat,右键单击我的项目,选择 Run As,选择 Tomcatv7.0 ,选择 Finish...

结果:

应用程序在日志中没有错误/异常启动,但我在浏览器中收到 404 错误和此警告:

警告:在 Disp 中找不到带有 URI [/thedallasapp_poc/] 的 HTTP 请求的映射 名称为“mvc_dispatcher”的 atcherServlet

通过 Maven 部署:

使用命令“mvn tomcat7:deploy”或“mvn tomcat7:redeploy”进行部署

再次,应用程序启动,日志中没有错误/异常,但我在浏览器中收到 404 错误和此警告:

警告:在 Disp 中找不到带有 URI [/thedallasapp_poc/] 的 HTTP 请求的映射 名称为“mvc_dispatcher”的 atcherServlet

最后我将 web.xml Dispatcher servlet-name 映射更改为“/thedallasapp_poc*”而不是“/”:

web.xml sn-p:

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

    <servlet-mapping>
        <servlet-name>mvc_dispatcher</servlet-name>
        <url-pattern>/thedallasapp_poc*</url-pattern>
    </servlet-mapping>

因此我也将 HomeController 从 @RequestMapping(value="/") 更改为 @RequestMapping(value="/thedallasapp_poc")

@控制器 公共类 HomeControler

public HomeControler() 
    super();
    // TODO Auto-generated constructor stub


/* serves the login landing/home page view */
@RequestMapping(value="/thedallasapp_poc")
public ModelAndView serveLanding(HttpSession session) 



    ModelAndView model = new ModelAndView();
    model.setViewName("login");
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    //check if isRememberMeAuthenticated cookie is true from previous login 
    System.out.println("Class HomeControler, String serveLanding(), isRememberMeAuthenticated():"+isRememberMeAuthenticated());
    if (isRememberMeAuthenticated()!=false && auth.isAuthenticated()!=false)    
    
        session.setAttribute("isRememberMeAuthenticated", true);

    

    return model;

//结束 HomeControler

然后我使用 Maven "mvn clean package" 重建了我的项目并再次启动了该项目。

结果: 我不再收到警告,但我仍然在浏览器中收到 404 错误,实际上我更喜欢 DispatcherServlet 映射的“/”映射。

非常感谢任何深思熟虑的建议/帮助,因为我在最后期限内。

谢谢

【问题讨论】:

你的应用有上下文路径吗? 为什么不扫描这个包 【参考方案1】:
<context:component-scan base-package="com.hitachi.thedallasapp_poc.Category"/>

上面一行将扫描com.hitachi.thedallasapp_poc.Category包内所有可用的组件

它不会扫描com.hitachi.thedallasapp_poc包内的组件。

同样,我可以看到你所有的 component scan 指的是 sub packagescom.hitachi.thedallasapp_poc 主包中

但未扫描主要包组件。

所以包含该行

<context:component-scan base-package="com.hitachi.thedallasapp_poc"/>

在你的 spring 配置文件中扫描这个包中的所有组件。

因此您的控制器会被 Spring 扫描和检测到。

【讨论】:

我通过命名每个包来引用所有包:&lt;context:component-scan base-package="com.hitachi.thedallasapp_poc.Category" /&gt; &lt;context:component-scan base-package="com.hitachi.thedallasapp_poc.location" /&gt; &lt;context:component-scan base-package="com.hitachi.thedallasapp_poc.messaging" /&gt; &lt;context:component-scan base-package="com.hitachi.thedallasapp_poc.userprofile" /&gt; &lt;context:component-scan base-package="com.hitachi.thedallasapp_poc.test" /&gt;,这相当于只使用基本包:&lt;context:component-scan base-package="com.hitachi.thedallasapp_poc" /&gt;,这是正确的。谢谢 嘿,您的 HomeController 类在 com.hitachi.thedallasapp_poc 包中,但是对其所有子包进行组件扫描无助于识别此控制器,您甚至必须包含此包以用于组件-扫描...希望你明白了..有任何疑问吗?

以上是关于Spring 4 MVC,警告:在 DispatcherServlet 中找不到带有 URI 的 HTTP 请求的映射,名称为 [...] 和 HTTP 404 错误的主要内容,如果未能解决你的问题,请参考以下文章

spring MVC + Ibatis 连接DB2 数据库 出现警告信息

Spring技术揭幕----DispatcherServlet

.NET 4 MVC 2 验证,带有注释警告而不是错误

网页未找到。 Spring MVC + 码头

购物车 Spring mvc

错误:没有找到带有 URI spring mvc 且没有 xml 的 HTTP 请求的映射