1

我有一个项目,我将 Magnolia cms 与 Spring mvc 集成。spring servlet 作为 magnolia 中的一个模块加载并分配了模式/web/*。servlet 在模块描述符中定义,因此 web.xml 文件中没有 servlet 定义,只有 magnolia 过滤器链在那里定义。

我遇到的问题是,当我尝试以下代码时

<jsp:include path="/web/header" />

例如,在 magnolia 页面模板中,我收到一个错误,它说它找不到文件并在/src/main/webapp/web/header.

但是如果我在 web.xml 中声明 spring servlet 没有错误并且它可以工作。

有人可以告诉我为什么会这样吗?为什么不能<jsp:include>只查看 magnolia 过滤器链并发现 spring servlet 映射该请求并返回请求的页面?

谢谢 :)

这是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <filter>
        <display-name>Magnolia global filters</display-name>
        <filter-name>magnoliaFilterChain</filter-name>
        <filter-class>info.magnolia.cms.filters.MgnlMainFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>magnoliaFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <context-param>
        <param-name>magnolia.initialization.file</param-name>
        <param-value>
          WEB-INF/config/${servername}/${webapp}/magnolia.properties, WEB-INF/config/${servername}/magnolia.properties,
          WEB-INF/config/${webapp}/magnolia.properties, WEB-INF/config/default/magnolia.properties,
          WEB-INF/config/magnolia.properties
        </param-value>
    </context-param>

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

</web-app>

这就是我在开花的模块描述符中定义spring servlet的方式:

    <servlets>
        <servlet>
            <name>Spring Servlet</name>
            <class>info.magnolia.module.blossom.web.InstallationAwareDispatcherServlet</class>
            <comment>Used for spring as dispatcher servlet</comment>
            <mappings>
                <mapping>/web/*</mapping>
            </mappings>
            <params>
                <param>
                    <name>contextConfigLocation</name>
                    <value>/WEB-INF/spring/appServlet/servlet-context.xml</value>
                </param>
            </params>
        </servlet>
    </servlets>

这是我在 magnolia 中用于主页主页的主要模板:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title>${content.title}</title>
    </head>

    <body>

        <jsp:include page="/web/header" />

    </body>
</html>

页 header.jsp 位于webapps/WEB-INF/views/.

它在包含旁边的 Eclipse 中向我显示的错误是:

在预期路径 /project_name/src/main/webapp/web/header 中找不到片段“/web/header”

它在浏览器中出现的错误是:

HTTP 状态 404 - /project_name/web/header

问题是,如果我将以下代码放在 web.xml 中,它可以工作:

    <servlet>
        <servlet-name>Spring Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Spring Servlet</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>

我希望这会有所帮助..非常感谢你:)

4

1 回答 1

1

Magnolia 过滤器链中的大多数过滤器默认不处理包含。在您的情况下,让 servlet 接受包含当然是有意义的。您可以通过更改 /server/filters/servlets/[your servlet] 中 servlet 的调度规则来启用此功能。

将 /server/filters/dispatching 复制到 /server/filters/servlets/[your servlet] 并将 dispatching/include/toMagnoliaResource 设置为 true。

Magnolia 将请求分类为针对 Magnolia 资源或 Web 容器资源,过滤器以不同方式处理它们。它们如何分类的配置在 /server/webContainerResources 中,所有与规则不匹配的东西都有一个 magnolia 资源。

于 2011-11-21T09:32:42.480 回答