我正在尝试使这个示例起作用: 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 内)。
我的项目是这样组织的:
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.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>
webapp-module-servlet.xml
<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>
hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
HelloController.java
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";
}
}
但是当我在 eclipse 中运行它时,我得到:
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'
我正在使用:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
网上有很多关于此建议的帖子,建议到处添加不同的 xml oneliner。我已经尝试了其中大部分,但仍然遇到相同的错误。
有什么建议吗?
最佳答案
更改 url 模式:
web.xml
<servlet-mapping>
<servlet-name>webapp-module</servlet-name>
<url-pattern>/</url-pattern>
<!--
<url-pattern>*.jsp</url-pattern>
-->
</servlet-mapping>
和组件扫描:
webapp-module-servlet.xml
<!--
<context:component-scan base-package="com.samples.*" />
-->
<context:component-scan base-package="com.samples" />
解决了问题。我现在得到:
关于java - org.springframework.web.servlet.PageNotFound noHandlerFound在eclipse中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36970818/