07-20-2023, 01:39 PM
I have a project in which I integrate Magnolia cms with Spring mvc. The spring servlet is loaded as a module in magnolia and is assigned the pattern `/web/*`. The servlet is defined in the module descriptor so there is no servlet definition in the web.xml file, there is only the magnolia filter chain defined there.
The problem I'm having is that when I try the following code
<jsp:include path="/web/header" />
for example in a magnolia page template, I get an error where it says it cannot find the file and it searches for it in `/src/main/webapp/web/header`.
But if I declare the spring servlet in web.xml there is no error and it works.
Can someone please tell me why is this happening? Why can't `<jsp:include>` just look through the magnolia filter chain and find that the spring servlet maps that request and give back the requested page?
Thanks :)
This is my web.xml file:
<?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
<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>
This is how I define the spring servlet inside the module descriptor for blossom:
<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>
This is the main template that I used for the page home in 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>
The page header.jsp is under `webapps/WEB-INF/views/`.
The error it shows me in eclipse next to the include is:
> Fragment "/web/header" was not found at expected path
> /project_name/src/main/webapp/web/header
The error that it comes up in the browser is:
> HTTP Status 404 - /project_name/web/header
The thing is that if I put the following code in web.xml it works:
<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>
I hope this helps.. Thank you very much :)
The problem I'm having is that when I try the following code
<jsp:include path="/web/header" />
for example in a magnolia page template, I get an error where it says it cannot find the file and it searches for it in `/src/main/webapp/web/header`.
But if I declare the spring servlet in web.xml there is no error and it works.
Can someone please tell me why is this happening? Why can't `<jsp:include>` just look through the magnolia filter chain and find that the spring servlet maps that request and give back the requested page?
Thanks :)
This is my web.xml file:
<?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
[To see links please register here]
;<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>
This is how I define the spring servlet inside the module descriptor for blossom:
<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>
This is the main template that I used for the page home in 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>
The page header.jsp is under `webapps/WEB-INF/views/`.
The error it shows me in eclipse next to the include is:
> Fragment "/web/header" was not found at expected path
> /project_name/src/main/webapp/web/header
The error that it comes up in the browser is:
> HTTP Status 404 - /project_name/web/header
The thing is that if I put the following code in web.xml it works:
<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>
I hope this helps.. Thank you very much :)