Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts 2 + Sitemesh 3 integration - NPE in FreemarkerDecoratorServlet

I am using Struts 2 (version 2.3.14.3) together with Sitemesh 3 (version 3.0-alpha-2) and I am getting a NullPointerException under cerain circumstances.

First, here is my struts2 / sitemesh configuration from web.xml:

<filter-mapping>
    <filter-name>struts-prepare</filter-name>
    <url-pattern>*.do</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>*.do</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

<filter-mapping>
    <filter-name>struts-execute</filter-name>
    <url-pattern>*.do</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

<servlet>
    <servlet-name>sitemesh-freemarker</servlet-name>
    <servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class>
    <init-param>
        <param-name>default_encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Now, here is the issue description:

If struts dev.mode is enabled and I try to access an action that does not exist, a struts ConfigurationException is thrown.

Struts dispatcher tries to handle the exception by calling org.apache.struts2.dispatcher.Dispatcher.sendError(),that writes the standard Struts error template (/org/apache/struts2/dispatcher/error.ftl) to the response.

Now this is when things start to go wrong.

Because the original request was decorated with sitemesh filter, even after the configuration error occured, sitemesh tries to apply a decorator, which results in an exception, because Struts action context isn't setup properly due to the configuration exception and I am getting the following:

java.lang.NullPointerException
org.apache.struts2.sitemesh.FreemarkerDecoratorServlet.process(FreemarkerDecoratorServlet.java:176)

Here is the appropriate code from FreemarkerDecoratorServlet.java - ctx variable is null:

ActionContext ctx = ServletActionContext.getActionContext(request);
model = freemarkerManager.buildTemplateModel(ctx.getValueStack(), ctx.getActionInvocation().getAction(), servletContext, request, response, wrapper);

I think that sitemesh shouldn't try to decorate the response in case there is an error when invoking the action, but I don't know how to do that without modifying the Struts2/Sitemesh integration.

Any ideas how to avoid the NPE?

Thanks, Marek

like image 861
Marek Cikanek Avatar asked Nov 12 '22 02:11

Marek Cikanek


1 Answers

A workaround could be putting this snippet at the end of the Struts.xml file:

<action name="pageNotFound">
    <result>404.jsp</result>
</action>

<action name="*">
    <result type="redirectAction">
        <param name="actionName">pageNotFound</param>
    </result>
</action>

it should intercept every undeclared, requested action and redirect them to an existing Action with its JSP.

Just a try... ;)

like image 142
Andrea Ligios Avatar answered Nov 15 '22 11:11

Andrea Ligios