java - spring mvc无法进入controller

 天涯flyer_948 发布于 2022-10-28 01:52
web.xml

    

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    Archetype Created Web Application

    
        contextConfigLocation
        /WEB-INF/config/spring/spring-ctx.xml
    

    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            /WEB-INF/config/spring/spring-mvc.xml
        
        1
    
    
        springmvc
        /*
    

    
        org.springframework.web.context.ContextLoaderListener
    
    

spring-ctx.xml

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

    

    

spring-mvc

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

    

    
    

controller

    package com.prs.dps;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    @Controller
    public class Test {
        @RequestMapping(value = "/toindex",method = RequestMethod.GET)
        public String toIndex(){
            return "index";
        }
    }
4 个回答
  • 堕天使008的解决方法是对的
    · DispatcherServlet加载Web组件的bean
    · ContextLoaderListener加载中间层和数据层的bean组件
    补充下
    · 建议把spring mvc项目中controller和service组件的包分开
    · 建议题主在spring-mvc.xml中加上静态资源处理器

    如果还没有解决问题,推荐阅读 spring mvc helloworld example 实例比较简明清楚,也可以下载实例项目

    2022-10-29 10:38 回答
  • <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    改为这个试下:

    <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    2022-10-29 10:40 回答
  • 修改spring-mvc.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <mvc:annotation-driven/>
        
        <context:component-scan base-package="com.prs.dps"/>
        <bean      
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"         p:prefix="/views/" p:suffix=".jsp" />
                <!-- 视图解释类 -->
        
        </beans>
    
    2022-10-29 10:41 回答
  • 这个项目里有两个容器。
    Spring application contextSpring 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">
        <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">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!--启用 MVC注解(@Controller,@RequestMapping)实现URL映射-->
    <mvc:annotation-driven/>
    2022-10-29 10:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有