我在Spring环境中尝试了一个非常简单的helloworld类RestFul webservice,我得到了“org.springframework.web.servlet.DispatcherServlet noHandlerFound”在DispatcherServlet中找到了带有URI [..]的HTTP请求的映射名称..“。我尝试了各种各样的东西,但没有任何工作。到目前为止,这就是我所拥有的:
的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>
Controller类;
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 DefaultAnnota I org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler将映射的URL路径[/partners/method=RequestMethod.GET]发送到处理程序'partnerController'上
但是当我尝试使用url访问时: 本地主机:9080 /上下文根/ testWs /合作伙伴/ 我收到错误: [11/28/12 9:31:07:034 EST] 00000044 PageNotFound W org.springframework.web.servlet.DispatcherServlet noHandlerFound在DispatcherServlet中找不到带有URI [/ contextRoot / testWs / partners /]的HTTP请求的映射,名称为' spitter'
非常感谢任何帮助。我根据论坛上的建议尝试了一些更改,但似乎没有任何效果。
由于
答案 0 :(得分:3)
您有@RequestMapping("method=RequestMethod.GET")
删除引号
@RequestMapping(method=RequestMethod.GET)
它应该有用。
答案 1 :(得分:1)
在服务器弹簧MVC项目执行时记住事情