我查找了几乎所有与该错误有关的主题,但没有一个对我有用。

我创建了一个简单的Spring MVC项目,当我运行该项目时出现以下错误:

févr. 19, 2016 12:29:56 PM org.springframework.web.servlet.PageNotFound noHandlerFound
AVERTISSEMENT: No mapping found for HTTP request with URI [/SpringMVCsample1/] in DispatcherServlet with name 'one'

这是我的代码:

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVCsample1</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>jsp/hello.jsp</welcome-file>
  </welcome-file-list>



   <display-name>Spring MVC Application</display-name>

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

   <servlet-mapping>
      <servlet-name>one</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

   </web-app>

一个servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>

<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">

    <!-- This will allow Spring to load all the components from package com.capgemini.springtest   -->
    <!-- and all its child packages. -->
   <context:component-scan base-package="com.capgemini.springtest" />

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

</beans>

OneController.java:
package com.capgemini.springtest;


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


@Controller
public class OneController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(ModelMap model) {

        model.addAttribute("message", "Hello Spring MVC Framework!");

        return "hello";
    }

}

hello.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>
   <h2>${message}</h2>
</body>
</html>

有任何想法吗 ?

最佳答案

当然,您将遇到这样的问题,因为您没有任何控制器来处理“/”请求,您要做的就是添加一个控制器或在Web文件夹下添加index.jsp

web
|--->WEB-INF
|    |--->jsp
|        |---->index.jsp
|--->index.jsp

10-06 08:53