没有找到带有URI SpringMVC Rest APi的HTTP请求的映射

时间:2015-09-09 09:21:38

标签: java spring spring-mvc tomcat servlets

我是Spring Framework的初学者。我正在尝试使用Eclipse IDE创建一个简单的Spring MVC项目并在Apache Tomcat上部署它但是我在终端中收到以下错误:

org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringProject/] in DispatcherServlet with name 'manaf'

Webbrowser显示404错误(http://localhost/FirstSpringProject/

这是我的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>FirstSpringProject</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

<servlet>
    <servlet-name>manaf</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>manaf</servlet-name>
    <url-pattern>/welcome.jsp</url-pattern>
    <url-pattern>/welcome.html</url-pattern>
    <url-pattern>*.html</url-pattern>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

这是我的manaf-servlet.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.manaf.controller.*" />
<bean id="viewResolver"
 class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
 value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

这是我的控制器

package com.manaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

@RequestMapping("/welcome")
public ModelAndView testMethod()
{
    String msg="Haai brothers";
    return new ModelAndView("welcome","message",msg);
}

}

1 个答案:

答案 0 :(得分:2)

web.xml编辑manaf servlet映射中,如下所示:

<servlet>
    <servlet-name>manaf</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>manaf</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

修改您的manaf-servlet.xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.manaf.controller" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
</beans>

现在,假设您在index.jsp处有 src/main/webapp/index.jsp ,您可以使用网址http://localhost/FirstSpringProject/访问它,并假设您有 welcome.jsp } {/ strong>,src/main/webapp/WEB-INF/welcome.jsp,您可以使用网址访问http://localhost/FirstSpringProject/welcome