2011-05-26 140 views
0

我試圖在一個小應用上實現Spring。 我得到以下幾點:簡單的Servlet映射

WARNING: No mapping found for HTTP request with URI [/audiClave/] in DispatcherServlet with name 'appServlet' 

這裏是我的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_3_0.xsd" 
    id="WebApp_ID" 
    version="3.0"> 
    <display-name>audiClave</display-name> 
<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-       class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet>  

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
</web-app> 

這裏是servlet-context.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:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    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"> 

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Scans within the base package of the application for @Components to configure as beans --> 
<!-- @Controller, @Service, @Configuration, etc. --> 
<context:component-scan base-package="com.audiClave.Service" /> 

<!-- Enables the Spring MVC @Controller programming model --> 
<mvc:annotation-driven /> 

</beans> 

控制器是:

package com.audiClave.Service; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 

/** 
* Handles requests for the application home page. 
*/ 
@Controller 
public class HomeController { 

@RequestMapping(value = "/") 
public String home() { 
    System.out.println("HomeController: Passing through..."); 
    return "WEB-INF/views/home.jsp"; 
} 
} 

http://localhost:8080/audiClave/個回報:

HTTP Status 404 
The requested resource() is not available. 

我eclipse下使用Tomcat 7.0.12和3.0.5春季運行。內容似乎正確地部署到E:\開發\ java.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ audiClave

當我改變映射到/ *它能夠找到控制器,但是然後home.jsp不起作用,因爲通配符匹配它

解決了當我再次將/ *映射出映射時。 謝謝

+0

是您的應用程序部署在 「的webapps/audiClave」? – Bozho 2011-05-26 12:40:11

回答

1

只做return "home"(而不是整個路徑到jsp)。應該將View處理程序配置爲使用該擴展名查找該文件夾中的視圖。

+0

感謝您的快速響應,但它似乎沒有幫助。我仍然得到26/05/2011 10:15:08 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在名爲'appServlet'的DispatcherServlet中找不到具有URI [/ audiClave /]的HTTP請求的映射 不要以爲它找到home()方法 – 2011-05-26 12:16:59

+0

對我有用..春天和tomcat的版本是什麼 – Bozho 2011-05-26 12:25:57

+0

Spring 3.0.2和tomcat 7.0 – 2011-05-26 12:38:55

0

的問題應該是這樣的:
在文件[文件名] -servlet.xml後綴請輸入以下行:
< mvc:resources location="/index.html" mapping="/index.html" />

+0

請把代碼放在代碼塊中 – demongolem 2012-12-27 17:15:30