Spring控制器请求映射未被识别

时间:2013-03-11 06:07:33

标签: java spring web-applications spring-mvc

我已经在一个侧面项目工作了一段时间,我仍然对spring框架有些新意。我注意到当我在tomcat服务器上编译Web应用程序时,永远不会识别请求映射。我已经确保正在使用组件扫描xml标签,但我不确定在哪里可以查找为什么它们没有被加载。这是github项目的链接:

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>

和我的spring-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>

如果我需要进一步发布,请告诉我。

非常感谢任何帮助。提前谢谢。

3 个答案:

答案 0 :(得分:1)

我无法检查您的本地问题的项目,下面的链接解释了您可能使用的不同类型的配置标记,例如mvc:annotation-drive ,context component-scan等。

Different Spring Annotation XML Declarations

我还建议您将log4j添加到项目中,然后将日志级别设置为debug。因此,当您启动项目时,它可以打印出@RequestMapping中的所有URL,当您向服务器发送请求时,它还可以打印出您发送的URL,这样您就可以轻松找到问题。

答案 1 :(得分: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>

答案 2 :(得分:0)

查看上下文xml文件,添加了组件扫描标记。但是缺少一个标签,使应用程序能够读取注释。

<annotation-driven/>

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

尝试添加缺少的标记。