当前位置: 首页 > 知识库问答 >
问题:

Spring :映射请求中出错(找不到带有 URI 的 HTTP 请求的映射)

曾飞雨
2023-03-14

很抱歉再次问这种问题,但我无法通过查看其他线程和Spring doc来解决我的问题。

我正在使用maven的3.1.0.RELEASE,并尝试使用注释和java配置。

以下是我的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_2_5.xsd" version="2.5">

<servlet>
    <servlet-name>WebAppServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/servletConf/web-application-config.xml<!-- ,
            /WEB-INF/servletConf/application-security.xml -->
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>WebAppServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

这是我的档案web-application-config.xml.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />

<context:spring-configured />

<context:component-scan base-package="testspring">
    <context:exclude-filter expression=".*_Roo_.*" type="regex" />
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

<mvc:annotation-driven />

我有两个类。第一个配置视图解析器

package testspring.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;



@Configuration
public class CostManagerConfig {

 // Resolve logical view names to .jsp resources in the /WEB-INF/jsp directory
    @Bean
    ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
        }
}

第二个定义我的控制器:

package testspring.web;

import java.util.Comparator;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Handles requests for the application home page.
 */
@Controller

public class HomeController {

/**
     * Default page when no mapping found.
     * @return
     */
    @RequestMapping("/*")
    public String home() {
        System.out.println("HomeController: Passing through...");
        return "home";
    }

}

根据我的配置,我想一切都应该指向我的home()函数。然而,事实并非如此,以下是Apache输出的日志:

DEBUG DispatcherServlet -名为“WebAppServlet”的DispatcherServlet正在处理[/testSpring/]的GET请求

DEBUG请求映射处理程序映射-查找路径的处理程序方法/DEBUG请求映射处理程序映射-没有找到[/]的处理程序方法

在名为“WebAppServlet”的DispatcherServlet中,未找到带有URI[/testSpring/]的HTTP请求的映射

调试调度程序Servlet -成功完成的请求

我试图更改@RequestMapping中的参数,但它不会改变任何东西......我尝试了以下方法

@RequestMapping("/* ")

@RequestMapping()

@请求映射(“/”)

@RequestMapping(“/home”)

@请求映射(“/home.html”)

我尝试访问的网址是

http://localhost:8080/testSpring/

http://localhost:8080/testSpring/home

http://localhost:8080/testSpring/home.html

你能试着帮我吗?

非常感谢您的阅读!

祝你愉快;)。

共有1个答案

禹智渊
2023-03-14

当然,您不能输入HomeControl ler,因为您将其排除在组件扫描之外:

<context:component-scan base-package="testspring">
  <context:exclude-filter expression=".*_Roo_.*" type="regex" />
  <context:exclude-filter expression="org.springframework.stereotype.
  Controller" type="annotation" />
</context:component-scan>

因此,只需评论此行:

<context:exclude-filter expression="org.springframework.stereotype.
Controller" type="annotation" />
 类似资料: