简体   繁体   English

如何将玉兰cms与spring mvc整合?

[英]how to integrate magnolia cms with spring mvc?

I'm trying to integrate my spring application with magnolia through the blossom module, but I'm having trouble understanding how to do this. 我正在尝试通过开花模块将我的spring应用程序与magnolia集成,但我无法理解如何执行此操作。

I've read alot on this subject, but I still don't actually know how my web.xml and applicationContext.xml should look like. 我已经阅读了很多关于这个主题的内容,但我实际上并不知道我的web.xml和applicationContext.xml应该是什么样子。

Should in my web.xml file have the 2 magnolia listeners and the spring listener and have 2 servlets one for blossom and one for spring? 我的web.xml文件中是否应该有2个玉兰听众和春天的听众,并且有两个servlet用于开花,一个用于春天? And then how should my applicationContext look like? 然后我的applicationContext应该怎么样?

Could you help me in giving me an example of a working integration of web.xml and applicationContext.xml? 你能帮我提一下web.xml和applicationContext.xml的工作集成示例吗? I've looked everywhere on the web but the web.xml is nowhere. 我在网上到处都看,但web.xml无处可去。

Thank you very much :) 非常感谢你 :)

There's a sample for Blossom that is a full webapp setup. 这是一个完整的webapp设置的Blossom 示例 Have a look at the web.xml and applicationContext.xml there. 看看那里的web.xml和applicationContext.xml。

The sample is based on the magnolia-empty-webapp project which is intended as a starting point that you can build on. 该示例基于magnolia-empty-webapp项目,该项目旨在作为您可以构建的起点。

In the sample you'll see that the usual Spring listener isn't in web.xml and that there's no DispatcherServlets in there either. 在示例中,您将看到通常的Spring侦听器不在web.xml中,并且那里也没有DispatcherServlets。 Instead the task of starting spring is done by the samples module. 相反,启动弹簧的任务由样本模块完成。 The module also creates a BlossomDispatcherServlet which is used to render templates and paragraphs in the rendering process. 该模块还创建一个BlossomDispatcherServlet,用于在呈现过程中呈现模板和段落。 The reason for this is that when Magnolia starts up it will go into install/update-mode and show the installation UI. 原因是当Magnolia启动时,它将进入安装/更新模式并显示安装UI。 At this point you don't want Spring to have been initialized because if you have beans that rely on Magnolia they will fail to start when Magnolia isn't ready. 此时你不希望Spring被初始化,因为如果你有依赖Magnolia的bean,它们将无法在Magnolia未准备好时启动。 So instead Spring is started by the module. 所以Spring就是由模块启动的。

However, if your beans are not going to depend on Magnolia than you can safely add Springs listener to web.xml and just start you BlossomDispatcherServlets from the module. 但是,如果您的bean不依赖于Magnolia,那么您可以安全地将Springs侦听器添加到web.xml,并从模块中启动BlossomDispatcherServlets。

Another thing that's probably helpful to know is that Magnolia renders using a Filter and that filter will process all requests that come in unless they have been excluded. 另一件可能有用的事情是,Magnolia渲染使用过滤器,该过滤器将处理所有进入的请求,除非它们已被排除。 So if you add servlets to web.xml you'll want to exclude their url-patterns from Magnolias filter. 因此,如果将servlet添加到web.xml,则需要从Magnolias过滤器中排除其url模式。

Another option which is much more straight forward is to add your servlet to your modules descriptor xml file instead. 另一个更直接的选择是将servlet添加到模块描述符xml文件中。 Then Magnolia will pick them up and call them from its filter. 然后Magnolia将接收它们并从其过滤器中调用它们。 The documentation for the module descriptor is here . 模块描述符的文档在这里

So in conclusion, I would recommend starting Spring using a module and adding servlets to the modules descriptor xml. 总而言之,我建议使用模块启动Spring并将servlet添加到模块描述符xml中。 Configuring things in web.xml is also a viable option but you need to take into account how that interacts with the install/update -phase and the request routing. 在web.xml中配置内容也是一个可行的选择,但您需要考虑如何与安装/更新-phase和请求路由交互。

Marius, 马吕斯,

The docs for Blossom touch on how to configure your web.xml file. Blossom文档介绍了如何配置web.xml文件。

Specifically, if you're starting Spring in your own module, you'd need to add this to your web.xml file before the Magnolia context listener: 具体来说,如果您在自己的模块中启动Spring,则需要在Magnolia上下文侦听器之前将其添加到web.xml文件中:

<listener>
<listener-class>info.magnolia.module.blossom.support.ServletContextExposingContextListener</listener-class>
</listener>

Additionally, you'd need to extend your module class to initialize and destroy the Blossom dispatcher servlets, something like so: 此外,您需要扩展模块类以初始化和销毁​​Blossom调度程序servlet,如下所示:

public class BlossomSampleModule extends BlossomModuleSupport implements ModuleLifecycle {

public void start(ModuleLifecycleContext moduleLifecycleContext) {
        initRootWebApplicationContext("classpath:/applicationContext.xml");
        initBlossomDispatcherServlet("blossom", "classpath:/blossom-servlet.xml");
    }

public void stop(ModuleLifecycleContext moduleLifecycleContext) {
        destroyDispatcherServlets();
        closeRootWebApplicationContext();
    }
}

Hope that helps a bit! 希望那有所帮助!

Sean 肖恩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM