Struts 2 + Sitemesh 3 integration - NPE in FreemarkerDecoratorServlet

一个人想着一个人 提交于 2019-12-12 11:10:53

问题


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


回答1:


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... ;)



来源:https://stackoverflow.com/questions/17260380/struts-2-sitemesh-3-integration-npe-in-freemarkerdecoratorservlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!