3

我正在 Spring 环境中尝试一个非常简单的 helloworld 类 RestFul Web 服务,我得到“org.springframework.web.servlet.DispatcherServlet noHandlerFound No mapping found for HTTP request with URI [..] in DispatcherServlet with name ..” . 我尝试了各种方法,但没有任何效果。到目前为止,这就是我所拥有的:

web.xml

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

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

<servlet-mapping>
    <servlet-name>spitter</servlet-name>
    <url-pattern>/testWs/*</url-pattern>
</servlet-mapping>

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

spitter-servlet.xml

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

        <mvc:annotation-driven />

        <context:component-scan base-package="org.resttest.bso" />

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>

</beans>

和控制器类;

package org.resttest.bso;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/partners")
public class PartnerController {

public PartnerController(){
    System.out.println("******* From the constructor ***** " );
}

@RequestMapping("method=RequestMethod.GET")
public String getPartner(ModelMap model){
    try{
        model.addAttribute("message", "Spring 3 MVC Hello World");
        System.out.println("******* Test message " );
    }catch(Exception ex){
        System.out.println("******* Exception thrown ... " + ex.getMessage());
    }
    return "test";
}
}

并在 WEB-INF/views/test.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Message : ${message}</h1>   
</body>
</html>

当服务器启动时,我也确实看到了:[11/28/12 9:24:02:803 EST] 00000013 DefaultAnota I org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 映射的 URL 路径 [/partners/method=RequestMethod .GET]到处理程序'partnerController'

但是当我尝试使用 url 访问时:localhost:9080/contextRoot/testWs/partners/ 我收到错误:[11/28/12 9:31:07:034 EST] 00000044 PageNotFound W org.springframework.web.servlet。 DispatcherServlet noHandlerFound 在名称为“spitter”的 DispatcherServlet 中找不到具有 URI [/contextRoot/testWs/partners/] 的 HTTP 请求的映射

非常感谢任何帮助。我根据论坛上的建议尝试了一些更改,但似乎没有任何效果。

谢谢

4

2 回答 2

3

你有@RequestMapping("method=RequestMethod.GET")

删除引号

@RequestMapping(method=RequestMethod.GET)

它应该工作。

于 2012-11-28T16:26:17.730 回答
1

记住服务器 spring MVC 项目做的事情

  1. index.jsp(第一个起始页)应该在 WebConent 下。不在WEB-INF 下不在您的jsp 文件夹(您的jsp 页面文件夹)下。
  2. 映射:url 模式就像,如果它在 web.xml 中就像 /
    那么“/”这个符号只出现在请求 mappin(“/ * *”) 的控制器类注释中。没有哪里来的。特别是在jsp页面的请求中。有要求去那个注释。
  3. 添加所有 jar,例如 ojdbc14.jar 用于 db。
  4. 自动装配是主要的,配置文件中的 bean id 名称应该与您的类中的名称相同。
于 2013-12-12T14:49:15.643 回答