2013-03-11 85 views
0

我一直在一個側面項目上工作了一段時間,而且我對Spring框架還是有點新的。我注意到當我在tomcat服務器上編譯Web應用程序時,請求映射永遠不會被識別。我已經確認組件掃描xml標籤正在被使用,但我不知道還有什麼地方可以尋找它們沒有被加載的原因。這裏的鏈接到GitHub的項目:Spring控制器請求映射不被識別

http://github.com/dmcquillan314/YouthMinistryHibernate.git

如這裏要求是我的web.xml

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>YouthMinistryHibernate</display-name> 

    <!-- Spring MVC --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 
    <context-param> 
     <param-name>spring.profiles.default</param-name> 
     <param-value>simple</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value></param-value> 
     </init-param> 
     <init-param> 
      <param-name>spring.profiles.default</param-name> 
      <param-value>simple</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- Spring Security --> 
    <filter> 
     <filter-name>encodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 
    <filter-mapping> 
     <filter-name>encodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <filter> 
     <filter-name>hiddenHttpMethodFilter</filter-name> 
     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>hiddenHttpMethodFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 

和我的彈簧mvc.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" 
    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-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <mvc:annotation-driven/> 

    <context:component-scan base-package="com.youthministry.controller" /> 

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

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basenames"> 
      <list> 
       <value>message</value> 
      </list> 
     </property> 
    </bean> 

</beans> 

讓我知道如果我需要進一步發佈任何內容。

任何幫助,非常感謝。提前致謝。

回答

1

我不能檢查你的項目爲我的本地問題,下面的鏈接說明你可以使用不同類型的配置標籤,如mvc:annotation-drive ,context component-scan等。

Different Spring Annotation XML Declarations

而且我也建議你的log4j添加到項目中,然後設置日誌級別調試。這樣當你啓動項目時,它可以打印出@RequestMapping中的所有url,並且當你向服務器發送請求時,它也可以打印出你發送的URL,所以你可以很容易地找到問題。

+0

我在註釋的配置中添加了它們,但它們似乎仍未被識別。我通過eclipse運行它。 – dmcqu314 2013-03-12 18:56:34

+0

是否向您的控制器類添加了@Controller? – OQJF 2013-03-13 00:32:26

+0

另一件事是您的servlet名稱是「appServlet」,因此您的config xml名稱應該是appServlet-mvc.xml,而不是spring-mvc.xml – OQJF 2013-03-13 00:36:15

0

查看您的上下文xml文件,您已添加組件掃描標籤。但是缺少一個標籤可以讓應用程序讀取註釋。

<annotation-driven/> 

<context:component-scan base-package="com.youthministry.controller" /> 

嘗試添加缺少的標記。

+0

我試過這個解決方案無濟於事。有任何想法嗎? – dmcqu314 2013-03-12 15:07:05

1

你的web.xml是什麼樣的? (也許你可以直接在這個帖子上發佈相關的配置文件....)路徑必須被指定爲「拾取」。

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html

<web-app> 

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

    <servlet-mapping> 
    <servlet-name>example</servlet-name> 
    <url-pattern>/example/*</url-pattern> 
    </servlet-mapping> 

</web-app>