2012-07-26 35 views
0

我在春天創建簡單的Hello World應用程序,它的工作就好了,然後我抄相同的文件,在另一個項目和所有突然主​​(只)控制器應用從未被調用春季控制器永遠不會被調用

網絡的.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>TestApp</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> 

    <!-- 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/appServlet-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> 

appServlet-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.testApp" /> 

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


    </beans> 

和HomeController的

package com.testApp.Controllers; 

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

問題是,我總是得到404錯誤和HomeController: Passing through...永遠不會被打印到控制檯,以便我懷疑的HomeController甚而不被調用

這是在另一個項目中工作正常,將打印HomeController: ...安慰,但我不能」噸得到的不成功嘗試得到這個工作,我決定創建新項目數小時後顯示,以便查看。現在看來甚至沒有控制器被調用

任何人都知道可能是什麼問題

+1

很可能你正在改變一些文件夾結構或其他東西。如果您使用的是eclipse,請使用import-export移動項目。 – JProgrammer 2012-07-26 23:35:20

+1

嘗試打開「org.springframework」的DEBUG級別日誌記錄。這應該告訴你什麼URL路徑被映射到哪些控制器/方法。 – nickdos 2012-07-27 05:21:59

回答

0

我不是當然,如果t可以幫助你,但你應該始終用小寫字母命名包,因爲它可能是一個混淆的來源,因爲在Windows機器上,因爲不同包的文件夾名稱是相同的(aAA和aaa都會進入一個文件夾) 。這又反過來影響自動掃描。

我在Weblogic服務器上遇到過這樣的問題:在服務器啓動時選擇了bean,但在應用程序重新部署組件掃描停止工作後,使某些bean(控制器)mysteriosly從工作應用程序中消失。根本原因是,首先,我在包名中輸入了一個拼寫錯誤,迫使weblogic在目錄名中用大寫字母創建文件夾結構。我手動刪除了可憐的軟件包目錄後,一切都被修復了(但是花了幾天的時間才找到問題的根源)。

Package naming convention

至於previos項目 - 可能 「WEB-INF \」(甚至 「\查看\」)的部分應被刪除,你可以可以聲明自己的視圖解析器指定的jsp前綴:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
0

我希望你只是訪問了錯誤的URL。你發佈的一切都很好。我把a sample project放在一起,使用你發佈的內容以及一個簡單的Home.jsp。你可以嘗試一下,看看它的工作原理與以下步驟:

git clone git://github.com/zzantozz/testbed.git tmp 
cd tmp/stackoverflow/11678699-basic-springmvc 
mvn jetty:run 

然後導航到http://localhost:8080/11678699-basic-springmvc/(對不起,#2不再允許使用「localhost」作爲主機創建超鏈接)。你會看到「你好」(Home.jsp),你會看到控制器打印到控制檯的消息。

相關問題