spring mvc无法进入controller

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 xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         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>Archetype Created Web Application</display-name>


    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/config/spring/spring-ctx.xml</param-value>

    </context-param>


    <servlet>

        <servlet-name>springmvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

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

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>springmvc</servlet-name>

        <url-pattern>/*</url-pattern>

    </servlet-mapping>


    <listener>

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

    </listener>

    </web-app>


spring-ctx.xml


    <?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:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="com.prs.dps"/>


    </beans>


spring-mvc



陪伴而非守候
浏览 1227回答 4
4回答

慕田峪7331174

这个项目里有两个容器。Spring application context 和 Spring webapplication context。分别对应两个配置文件applicationContext.xml 和 {servletName}-servlet.xml。他们之间并不会共享管理的对象。通过你的配置文件可以看出只有根容器Spring application进行了扫描, Spring MVC的容器(webapplication context)中没有管理的对象。而 Spring 根容器(application context)不具备处理映射的功能,无法处理请求映射。所以配置应该这样。// spring-ctx.xml....// 根容器不扫描@Controller注解的类。<context:component-scan base-package="gq.zpf_fly.first">&nbsp; &nbsp; <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>...// spring-mvc.xml....// 不是用默认过滤规则(指定包内全部扫描), 手动设置规则,只扫描@Controller注解的类。<context:component-scan base-package="gq.zpf_fly.first" use-default-filters="false">&nbsp; &nbsp; <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--启用 MVC注解(@Controller,@RequestMapping)实现URL映射--><mvc:annotation-driven/>

蛊毒传说

<servlet-mapping>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>springmvc</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <url-pattern>/*</url-pattern></servlet-mapping>改为这个试下:<servlet-mapping>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>springmvc</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <url-pattern>/</url-pattern></servlet-mapping>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java