【问题标题】:Strange behavior of @RequestMapping on a maven war overlay@RequestMapping 在 Maven 战争覆盖上的奇怪行为
【发布时间】:2013-09-01 01:41:27
【问题描述】:

@RequestMapping 行为有一些奇怪的问题。这是场景

我有一个带有@Controller 类和一些@RequestMapping 方法的Web 项目。他们工作正常。例如,一种映射是 @RequestMapping("/jpm/{param}/add")

我有另一个 web 项目,第一个项目上有战争覆盖。一切正常,我继承了控制器及其方法。目前没问题。

现在我想在第二个项目中添加一个新的@Controller。我像另一个一样添加它,但使用了不同的 @RequestMapping ("/jpm/{param}/activate") 方法。

@Controller 和 @RequestMapping 被 Spring 读取得很好,因为我可以在日志中看到它们,但是,虽然 "/jpm/{param}/add" 有效,但 "/jpm/{param}/activate" 没有,它说404 资源未找到。

两个类都在不同的包中,但都加载了,我有两个上下文:组件扫描,每个项目一个。我发现如果两个类都在同一个包(名称)中,它们可以正常工作(即使在不同的项目中)。

这正常吗?我错过了什么?为什么控制器和映射被读取但不起作用?

我对此很生气,欢迎任何建议!

web.xml(第一个项目)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/applicationContext.xml, 
        WEB-INF/spring-locale.xml, 
        WEB-INF/spring-security.xml,
        WEB-INF/spring-datasource.xml,
        WEB-INF/spring-hibernate.xml,
        WEB-INF/spring-jpm.xml
    </param-value>
</context-param>

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

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>

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

这是我的 MVC 配置文件,主要是标准的东西

<?xml version="1.0" encoding="UTF-8"?>
<!-- this file will contain all of JPM Spring Web MVC-specific components -->
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    ...
    <context:component-scan base-package="jpaoletti.jpm2.controller"/>
    ...
</beans>

*最终编辑:解决方案 *

仅用于文档:

在第一个项目中,我添加了 servlet 配置文件 (jpm-servlet.xml)

<import resource="jpm-servlet-custom.xml" />

在第二个项目中:

jpm-servlet-custom.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        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">

    <context:component-scan base-package="another.package.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

</beans>

【问题讨论】:

  • 请提供您的web.xml
  • Sotirios Delimanolis 好了
  • 你说两个类都在不同的包中,但都加载了,我有两个上下文:组件扫描,每个项目一个。我发现如果两个类都在同一个包(名称)中,它们可以正常工作(即使在不同的项目中)第二个项目的上下文是否加载到任何地方?
  • 是的,在第二个项目中,我覆盖了“spring-jpm.xml”文件,添加了第二个上下文:component-scan ...这让我认为它没有被加载到 dispathcer servlet ...您可能有一个观点,对吧?
  • 好吧,如果没有加载该上下文,context:component-scan 将毫无用处。从您的servlet-jpm.xml 导入它。

标签: java spring maven spring-mvc


【解决方案1】:

根据你的问题

两个类都在不同的包中,但都加载了,我有两个 上下文:组件扫描,每个项目一个。我发现如果两者 类在同一个包(名称)中,它们工作正常(即使在 不同的项目)。

似乎第二个项目的 MVC 上下文要么根本没有加载,要么没有被您的应用程序的 DispatcherServlet 加载。因此component-scan 没有加载第二个项目@Controllers。

确保 DispatcherServlet. 正在加载上下文

【讨论】:

  • spring-jpm.xml 没有任何与 mvc 相关的 bean。我还有一个 jpm-servlet 文件(由 DispathcerServlet 自动加载)
  • @jpaoletti 我会删除我的答案。然后我们需要查看您的 MVC 配置。
  • 用最终解决方案编辑了问题。非常感谢你们!
【解决方案2】:

两个类都在不同的包中,但都已加载,我有两个上下文:组件扫描,每个项目上一个。我发现如果两个类都在同一个包(名称)中,它们可以正常工作(即使在不同的项目中)。

我认为您可能已经确定了问题的根本原因......


请注意,一旦您将所有内容组合到一个 WAR 文件中,“项目”就会变得无关紧要。在执行时,类(等)要么通过类路径可见......要么不可见。在 Maven 覆盖完成后,您可能还会对有效的 Spring 配置有些困惑。 (去过那里,做到了。)因此,请确保检查 已部署 webapp 中的配置,并根据 它们 上发生的事情进行推理。


根据证据,我认为问题在于您的一个组件扫描不起作用/没有发生。因此,注释处理器看不到“丢失”控制器的注释。

作为一个实验,我会尝试“破解”Spring 配置(首先在已部署的 web 应用程序上)将两个单独的组件扫描组合成一个扫描所有相关包的单个组件。如果可行,那么请弄清楚如何使用 Maven 覆盖来实现相同的最终结果。

【讨论】:

  • 遵循 Sotirios Delimanolis 的指导和这个想法,我得到了解决方案。非常感谢你们!
猜你喜欢
  • 2011-08-03
  • 2014-02-04
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
  • 2013-03-31
  • 1970-01-01
  • 2021-06-09
相关资源
最近更新 更多