2

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

我正在使用 3.1.0.RELEASE 和 maven 并尝试使用注释和 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 输出的日志并非如此:

调试 DispatcherServlet - 名称为“WebAppServlet”的 DispatcherServlet 处理 [/testSpring/] 的 GET 请求

DEBUG RequestMappingHandlerMapping - 查找路径的处理程序方法 / DEBUG RequestMappingHandlerMapping - 未找到 [/] 的处理程序方法

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

DEBUG DispatcherServlet - 成功完成请求

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

@RequestMapping("/*")

@RequestMapping()

@RequestMapping("/")

@RequestMapping("/home")

@RequestMapping("/home.html")

我试图到达的网址是

http://localhost:8080/testSpring/

http://localhost:8080/testSpring/home

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

你能帮帮我吗?

非常感谢您的阅读!

祝你今天过得愉快;)。

4

1 回答 1

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" />
于 2012-08-15T19:32:11.900 回答