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

时间:2012-08-15 18:03:20

标签: java spring

很抱歉再次提出这样的问题,但我无法解决我在查看其他主题和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 RequestMappingHandlerMapping - 查找路径的处理程序方法/ DEBUG RequestMappingHandlerMapping - 没有为[/]

找到处理程序方法

WARN PageNotFound - 在名为'WebAppServlet'的DispatcherServlet中找不到带有URI [/ testSpring /]的HTTP请求的映射

DEBUG DispatcherServlet - 已成功完成请求

我试图更改@RequestMapping中的参数,但它没有改变任何东西......我试过以下

@RequestMapping( “/ *”)

@RequestMapping()

@RequestMapping( “/”)

@RequestMapping( “/家”)

@RequestMapping( “/ home.html做为”)

我试图访问的网址是

http://localhost:8080/testSpring/

http://localhost:8080/testSpring/home

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

你可以试着帮助我吗?

非常感谢您的阅读!

度过愉快的一天;)。

1 个答案:

答案 0 :(得分:2)

当然,您无法进入HomeController,因为您已将其从组件扫描中排除:

<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" />