2016-11-06 255 views
0

我看過這篇文章沒有解決方案。我試圖運行一個Spring MVC教程,但由於某種原因,我無法獲得我的servlet中的映射來調用在/ WEB-INF/jsps目錄中找到的home.jsp。我的上下文根被設置爲在Eclipse Web項目設置中彈出。我得到的錯誤是:Spring MVC在DispatcherServlet中找不到具有URI/x /的HTTP請求的映射

INFO 2016-11-06 11:18:59,613 [http-bio-8080-exec-3] 
     org.springframework.web.servlet.DispatcherServlet - 
     FrameworkServlet 'offers': initialization completed in 486 ms 
    WARN 2016-11-06 11:18:59,623 [http-bio-8080-exec-3] 
    org.springframework.web.servlet.PageNotFound 
    - No mapping found for HTTP request with URI [/spring/] 
    in DispatcherServlet with name 'offers' 

我的代碼如下:

的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" 
      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>spring-tutorial-50</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> 
     <description></description> 
     <display-name>offers</display-name> 
     <servlet-name>offers</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <web:load-on-startup>1</web:load-on-startup> 
     </servlet> 
     <servlet-mapping> 
     <servlet-name>offers</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:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
      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- 
      3.2.xsd"> 

     <context:component-scan base-package="com.mypackage.spring.web.controllers"> 
     </context:component-scan> 
     <mvc:annotation-driven /> 

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

    </beans> 

控制器:

@Controller 
    public class OffersController { 

     private Logger log = Logger.getLogger(OffersController.class); 

     @RequestMapping("/") 
     public String showHome(){ 

      log.info("showHome() called"); 
      return "home"; 

     } 
    } 

回到Home.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>Inside jsps</title> 
    </head> 
    <body> 
    Hi Mom! 
    </body> 
    </html> 

回答

0

我剛剛升級到3.2.17春季爲我所有的Maven庫引用。當我清理並重新部署時它工作正常!如果服務器配置正確,@RequestMethod(「/」)本身將工作。還可以更明確地瞭解RequestMethod可以如何處理什麼:@RequestMethod(value =「/ spring」,method = RequestMethod.GET)作爲@javaguy列表。值=「/」等也可以。

0

你沒有/spring URI映射到控制器RequestMapping,看看下面的代碼正確的映射:

@Controller 
    public class OffersController { 

     private Logger log = Logger.getLogger(OffersController.class); 

     //Map RequestMapping to /spring 
     @RequestMapping("/spring", method=RequestMethod.GET) 
     public String showHome(){ 

      log.info("showHome() called"); 
      return "home"; 

     } 
    } 
+0

在Controller @RequestMapping()中試過「/ spring」。這是行不通的。原來的警告仍然存在。從我讀過的其他帖子/應該服務所有請求。我已經嘗試過/ spring和/ spring /的變體,無濟於事。 – blyons

+0

向@RequestMapping添加method = RequestMethod.GET,試試 – developer

相關問題