Struts 2-拦截器init()中servletContext为空

时间:2018-07-17 16:13:58

标签: struts2

我试图在我的自定义Interceptor init()中获取servletContext或http请求,以设置一些特定于应用程序的详细信息。我已经搜索了Struts Interceptors文档和所有相关链接,但没有运气 :(。以下是我的Struts.xml和Interceptor类代码段。任何帮助都非常明显。谢谢。。

Struts.xml

<struts>
<constant name="struts.custom.i18n.resources" value="global"></constant>
<constant name="struts.ui.theme" value="simple" />

<package name="csr" extends="struts-default">

    <interceptors>
       <interceptor name="ppeLogger" class="java4s.InterceptorTest"></interceptor>

        <interceptor-stack name="PPELog">
            <interceptor-ref name="servletConfig" />
            <interceptor-ref name="ppeLogger"/>
        </interceptor-stack>
    </interceptors>

</package>

InterceptorTest.java

public class InterceptorTest implements Interceptor, ServletContextAware{

private static final long serialVersionUID = 1L;
private ServletContext servletContext;
private HttpServletRequest request = null;

public void destroy() {
    //Code
}

public void init() {

    request = (HttpServletRequest) ServletActionContext.getRequest // request is null

    servletContext = ServletActionContext.getServletContext(); //servletContext is null

}

}

0 个答案:

没有答案