2013-03-11 84 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>