简体   繁体   English

Spring Maven DispatcherServlet noHandlerFound

[英]Spring Maven DispatcherServlet noHandlerFound

I am new to java world. 我是Java世界的新手。 I am following one tutorial to learn java. 我正在按照一个教程学习Java。 The tutorial using Spring framework and maven build. 本教程使用Spring框架和Maven构建。 but when I reached the part to use DispatcherServlet, mine doesn't work as it suppose. 但是当我到达使用DispatcherServlet的部分时,我的却无法正常工作。 It shows this on console: 它在控制台上显示:

org.springframework.web.servlet.PageNotFound noHandlerFound 

WARNING: No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'dispatcher'

and browser can't load just simple hello world string. 而且浏览器无法仅加载简单的hello world字符串。 browser says 浏览器说

HTTP ERROR 404
Problem accessing /index.xml. Reason:
Not Found
Powered by Jetty://

This seems common problem becasue there are a lot of discussions about this. 这似乎是一个常见的问题,因为对此有很多讨论。 I have read several of them and tried some answers but nothing helps. 我已经阅读了其中的一些内容,并尝试了一些答案,但没有帮助。 Can anyone check my code and point out what might cause this problem? 谁能检查我的代码并指出可能导致此问题的原因?

web.xml web.xml中

<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>TestApplication</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>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>
</servlet-mapping>
</web-app>

dispacher-servlet.xml dispacher-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="my.testApplication.controller" />
</beans>

IndexController.java IndexController.java

package my.testApplication.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

@Controller
public class IndexController {

  @RequestMapping("/index")
  public String index(){
    return "WEB-INF/jsp/index.jsp";
  }
}

index.jsp 的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

hello from spring web mvc
</body>
</html>

Additional comment (solution) : it was right reference, I made some mistakes and then it didn't work afterwards even with the right code. 附加注释(解决方案) :是正确的参考,我犯了一些错误,但是即使使用正确的代码,此后也无法使用。 when this kind of situation (after mistakes and back to right code - still not working), then need to perform 'Project - clean' before run. 当出现这种情况时(在出错并返回正确的代码后-仍然无法正常工作),则需要在运行之前执行“项目-清理”。 (maybe the basics.) So that solved. (也许是基础知识。)这样就解决了。 Thanks. 谢谢。

It should be @RequestMapping annotation. 它应该是@RequestMapping注释。

@RequestMapping(value="/index")
  public String index(){
    return "WEB-INF/jsp/index.jsp";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DispatcherServlet noHandlerFound - DispatcherServlet noHandlerFound Spring MVC with EXT JS error:DispatcherServlet noHandlerFound - Spring MVC with EXT JS error: DispatcherServlet noHandlerFound org.springframework.web.servlet.DispatcherServlet noHandlerFound用于基本的Spring示例 - org.springframework.web.servlet.DispatcherServlet noHandlerFound for basic spring example org.springframework.web.servlet.DispatcherServlet noHandlerFound:Spring MVC - org.springframework.web.servlet.DispatcherServlet noHandlerFound : Spring MVC 带有 maven 的 Spring mvc 给出 noHandlerFound 错误 - Spring mvc with maven giving noHandlerFound error 在Spring Maven项目中找不到DispatcherServlet - DispatcherServlet not found in spring maven project 获取.servlet.DispatcherServlet noHandlerFound - Getting .servlet.DispatcherServlet noHandlerFound 如何解决Spring框架项目错误org.springframework.web.servlet.DispatcherServlet noHandlerFound? - How to resolve Spring framework project error org.springframework.web.servlet.DispatcherServlet noHandlerFound? 资源处理程序Spring异常:org.springframework.web.servlet.DispatcherServlet noHandlerFound - resource handler Spring exception: org.springframework.web.servlet.DispatcherServlet noHandlerFound org.springframework.web.servlet.DispatcherServlet noHandlerFound - org.springframework.web.servlet.DispatcherServlet noHandlerFound
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM