Struts 2 + Sitemesh 3集成-FreemarkerDecoratorServlet中的NPE [英] Struts 2 + Sitemesh 3 integration - NPE in FreemarkerDecoratorServlet

查看:182
本文介绍了Struts 2 + Sitemesh 3集成-FreemarkerDecoratorServlet中的NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Struts 2(版本2.3.14.3)与Sitemesh 3(版本3.0-alpha-2)一起使用,在某些情况下,我得到了NullPointerException.

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.

首先,这是来自web.xml的struts2/sitemesh配置:

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>

现在,这是问题说明:

如果启用了struts dev.mode,而我尝试访问一个不存在的操作,则会引发struts ConfigurationException.

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

Struts调度程序尝试通过调用org.apache.struts2.dispatcher.Dispatcher.sendError()处理异常,该标准将标准Struts错误模板(/org/apache/struts2/dispatcher/error.ftl)写入响应

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.

因为原始请求是用sitemesh过滤器修饰的,所以即使在发生配置错误后,sitemesh也会尝试应用修饰符,这会导致异常,因为由于配置异常,Struts操作上下文未正确设置,我得到以下内容:

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)

这是FreemarkerDecoratorServlet.java中的适当代码-ctx变量为空:

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

我认为sitemesh不应该尝试修饰响应,以防在调用操作时出现错误,但是我不知道如何在不修改Struts2/Sitemesh集成的情况下做到这一点.

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.

任何想法如何避免NPE?

Any ideas how to avoid the NPE?

谢谢, 马雷克

推荐答案

一种解决方法可能是将此代码段 放置在Struts.xml文件的末尾 :

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>

它应拦截所有未声明的请求动作,并将其重定向到具有其JSP的现有动作.

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

只需尝试...;)

这篇关于Struts 2 + Sitemesh 3集成-FreemarkerDecoratorServlet中的NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆