eclipse中的org.springframework.web.servlet.PageNotFound noHandlerFound

时间:2016-05-01 19:13:54

标签: java eclipse spring maven servlets

我想让这个例子有用: http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm

在ubuntu 15.10上使用eclipse版本:Mars.2 Release(4.5.2),java 8,maven和tomcat 8(在eclipse中)。

我的项目组织如下:

4

其中:

的web.xml

webapp (maven parent pom)
webapp-module (maven war sub module)
-> src
  -> main
    -> java
      -> com     
        -> samples
          -> HelloController.java
    -> webapp
      -> WEB-INF
        -> jsp
          -> hello.jsp
        -> web.xml
        -> webapp-module-servlet.xml

web应用模块-servlet.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 MVC Application</display-name>

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

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

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

   <servlet-mapping>
      <servlet-name>webapp-module</servlet-name>
      <url-pattern>*.jsp</url-pattern>
   </servlet-mapping>

</web-app>

的hello.jsp

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   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">

<context:annotation-config/>

   <context:component-scan base-package="com.samples.*" />

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

HelloController.java

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

但是当我在日食中运行时,我得到:

package com.samples;

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

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

enter image description here

我正在使用:

INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
May 01, 2016 9:35:45 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
May 01, 2016 9:35:45 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
May 01, 2016 9:35:45 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
May 01, 2016 9:35:45 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Sun May 01 21:35:45 CEST 2016]; root of context hierarchy
May 01, 2016 9:35:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/webapp-module-servlet.xml]
May 01, 2016 9:35:45 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 657 ms
May 01, 2016 9:35:45 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'webapp-module'
May 01, 2016 9:35:45 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'webapp-module': initialization started
May 01, 2016 9:35:45 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'webapp-module-servlet': startup date [Sun May 01 21:35:45 CEST 2016]; parent: Root WebApplicationContext
May 01, 2016 9:35:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/webapp-module-servlet.xml]
May 01, 2016 9:35:46 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'webapp-module': initialization completed in 313 ms
May 01, 2016 9:35:46 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
May 01, 2016 9:35:46 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
May 01, 2016 9:35:46 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2241 ms
May 01, 2016 9:35:46 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/webapp-module/] in DispatcherServlet with name 'webapp-module'
May 01, 2016 9:35:53 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/webapp-module/hello] in DispatcherServlet with name 'webapp-module'

网上有很多帖子关于这个建议在这里和那里添加不同的xml oneliner。我已经尝试了大部分但仍然遇到同样的错误。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

更改网址格式:

<强>的web.xml

   <servlet-mapping>
      <servlet-name>webapp-module</servlet-name>
      <url-pattern>/</url-pattern>
      <!--
      <url-pattern>*.jsp</url-pattern> 
       -->
   </servlet-mapping>

分量扫描:

<强> web应用模块-servlet.xml中

   <!--  
   <context:component-scan base-package="com.samples.*" />
    -->

   <context:component-scan base-package="com.samples" />

修正了问题。我现在得到:

enter image description here

答案 1 :(得分:0)

根据您提供的详细信息,似乎缺少控制器。控制器是接收来自客户端的请求的组件,更新模型对象并将控件传递给视图解析器,以便可以呈现页面。