HSGApple 2024-07-19 21:11 采纳率: 0%
浏览 17
已结题

Spring MVC项目,访问不到相应的控制器方法

项目描述

一个简单的Spring MVC项目,关键代码如下:
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:SpringApplicationContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/m/*</url-pattern>
    </servlet-mapping>
</web-app>

SpringApplicationContext.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.mycompany"/>
    
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain; charset=utf-8</value>
                    </list>
                </property>
            </bean>
            <bean class="com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json; charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <mvc:view-resolvers>
        <mvc:content-negotiation>
            <mvc:default-views>
                <bean class="com.alibaba.fastjson2.support.spring.webservlet.view.FastJsonJsonView"/>
            </mvc:default-views>
        </mvc:content-negotiation>
        <ref bean="thymeleafViewResolver"/>
    </mvc:view-resolvers>
</beans>

ThymeleafConfig

@Configuration
open class ThymeleafConfig {
    @Bean
    open fun springResourceTemplateResolver(): SpringResourceTemplateResolver =
        SpringResourceTemplateResolver().apply {
            templateMode = TemplateMode.HTML
            isCacheable = true
            order = 1
            characterEncoding = "UTF-8"
            prefix = "/WEB-INF/templates/"
            suffix = ".html"
        }

    @Bean
    open fun springTemplateEngine(): SpringTemplateEngine =
        SpringTemplateEngine().apply {
            setTemplateResolver(springResourceTemplateResolver())
        }

    @Bean
    open fun thymeleafViewResolver(): ThymeleafViewResolver =
        ThymeleafViewResolver().apply {
            order = 1
            characterEncoding = "UTF-8"
            templateEngine = springTemplateEngine()
        }
}

PathPatternParserConfig

@Configuration
@EnableWebMvc
open class PathPatternParserConfig : WebMvcConfigurer {
    override fun configurePathMatch(configurer: PathMatchConfigurer) {
        configurer
            .setPatternParser(PathPatternParser())
    }
}

UrlController

@Controller
@RequestMapping("/m")
class UrlController {
    
    @GetMapping("")
    fun index() = "index"
    
    @GetMapping("/hello")
    fun hello(): String = "hello"
}

操作环境

apache-tomcat-9.0.85
Java 17
Spring 5.3.34

问题描述

访问:http://localhost:8080/m 成功
访问:http://localhost:8080/m/hello 失败, 如下图

img

请问问题出在哪里?

  • 写回答

14条回答 默认 最新

  • 阿里嘎多学长 2024-07-19 21:11
    关注

    以下内容由AIGC及阿里嘎多学长共同生成、有用望采纳:


    根据您提供的项目配置信息和问题描述,这里有几个可能的原因导致您的Spring MVC项目无法访问到相应的控制器方法:

    1. URL模式匹配问题

      • 您的web.xml配置文件中<url-pattern>设置为/m/*,这意味着所有以/m/开头的请求都会映射到DispatcherServlet。但是,如果您访问的是/m/hello,它应该能够正确匹配到UrlController中的@GetMapping("/hello")方法。如果这个方法没有被调用,可能存在其他配置问题。
    2. 组件扫描问题

      • 检查SpringApplicationContext.xml中的<context:component-scan>标签是否包含了UrlController所在的com.mycompany包。如果UrlController不在扫描路径下,它将不会被Spring MVC框架识别。
    3. 控制器和方法注解问题

      • 确保UrlController类使用了@Controller注解,并且@RequestMapping("/m")注解正确地定义了基础路径。同时,@GetMapping("/hello")注解也应该没有问题。
    4. Spring MVC配置问题

      • 您的SpringApplicationContext.xml配置文件中的<mvc:annotation-driven>标签配置了消息转换器,但没有配置<mvc:default-servlet-handler>,这可能会导致静态资源处理问题。尝试添加如下配置:
        <mvc:default-servlet-handler/>
        
      • 另外,您使用了FastJsonHttpMessageConverterFastJsonJsonView,确保这些依赖和配置是正确的,并且与Spring MVC兼容。
    5. Web服务器配置问题

      • 您使用的是Apache Tomcat 9.0.85,确保Tomcat服务器的配置没有问题,比如server.xml中的端口和路径设置。
    6. 异常处理问题

      • 您提供的404错误页面截图显示了HTTP状态 404 - 未找到,这可能意味着请求没有被正确处理。检查Spring MVC的异常处理器是否配置正确,或者是否有其他异常处理器捕获了这个异常。
    7. 日志和调试

      • 启用Spring MVC的详细日志记录,查看请求处理的详细过程,这有助于定位问题。
    8. 代码和配置文件检查

      • 检查是否有任何拼写错误或遗漏的配置项,这些都可能导致映射失败。

    如果上述步骤都无法解决问题,建议逐步调试或使用日志记录来跟踪请求的处理流程,以便找到问题的具体原因。同时,您也可以考虑创建一个简单的控制器和方法来测试基本的Spring MVC功能是否正常工作,然后逐步添加复杂性。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月20日
  • 创建了问题 7月19日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见