404错误Spring MVC

时间:2012-08-06 20:39:31

标签: java web-applications servlets spring-mvc

我编写了一个Spring MVC应用程序,它显示了在XML视图中运行的测试用例和套件的报告。我编写了很好的模型类,我编写了以下文件,但是我觉得Handler Mapping存在一些问题

我的controller.java类看起来像这样

package com.DrAssist.Fitnesse.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.DrAssist.Fitnesse.model.Suite;



@Controller
@RequestMapping("/Suite/{name}")
public class XMLController {

    @RequestMapping(value="{name}", method = RequestMethod.GET, headers = "content-type=application/xml")
    public @ResponseBody Suite getSuiteInXML(@PathVariable String name) {

        Suite suite = new Suite("1",name,"3","Test1","4","5","7","9","550");

        return suite;

    }

}

我的web.xml文件如下所示

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

和dispatcher-servlet.xml文件如下所示

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.DrAssist.Fitnesse.controller" />

    <mvc:annotation-driven />

    <!--  
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

    <bean id="xmlViewer" 
        class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg>
            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                <property name="classesToBeBound">
                    <list>
                        <value>com.DrAssist.Fitnesse.model</value>
                    </list>
                </property>
            </bean>
        </constructor-arg>
    </bean>
    -->

</beans>

我收到404错误,控制台显示错误为

在DispatcherServlet中找不到带有URI [/ SpringMVC / Suite / suite1]的HTTP请求的映射,名称为“mvc-dispatcher”

请咨询似乎请求无法从/ SpringMVC / Suite / suite1的SpringMVC部分获取。

提前致谢

4 个答案:

答案 0 :(得分:0)

您将{name}路径变量放在@RequestMapping中。尝试将第一个保留为@RequestMapping("Suite")

答案 1 :(得分:0)

您的代码将无效,因为日志语句是用DispatcherServlet编写的,并且在那里编写得非常好。除了复制DispatcherServlet替换生成错误响应代码的代码而是将其重定向到所需的输出之外,你几乎无能为力。

答案 2 :(得分:0)

在web.xml中,您将调度程序声明为mvc-dispatcher,并且您提供的spring配置文件名是dispatcher-servlet.xml,它应该是mvc-dispatcher-servlet.xml。试试这个。

答案 3 :(得分:0)

如果您已经写过

@RequestMapping(value="{name}"

你不需要写这个,这是错误的。请改变

@RequestMapping("/Suite/{name}") 

改为写

 @RequestMapping("/Suite")