java - Spring Controller 请求映射未被识别

标签 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>

如果我需要进一步发布任何内容,请告诉我。

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

最佳答案

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

Different Spring Annotation XML Declarations

而且我还建议您将log4j添加到项目中,然后将日志级别设置为调试。这样当你启动项目时它可以打印出@RequestMapping中的所有url,当你向服务器发送请求时,它也可以打印出你发送的url,这样你就可以很容易地发现问题。

关于java - Spring Controller 请求映射未被识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15332200/

相关文章:

javascript - 如何使用Thymeleaf的blur/onblur事件来调用函数?

javascript - 如何处理 JavaScript 中的唯一 API key ?

java - 创建 EntityManager + Junit + HSQLDB 的问题

Java FileInputStream 在 Windows 和 Linux 中表现出不同的行为

java - 将 Spring Transaction 与 Mybatis 批处理结合使用

java - Spring Security BCrypt 密码编码器 - 工作负载因素

java - 我的 Hibernate 拦截器不工作

asp.net - 如何将 ASP.NET 网站转换为 ASP.NET Web 应用程序

web-applications - 如何设计/规划 Web 应用程序开发?

java - 如何查找两个日期之间差异的持续时间