日期:2014-05-19  浏览次数:20647 次

怎样获取action的name或者method
我想实现一个功能,就是在页面每次发起请求的时候,我通过过滤器来获取这个action的name或者method。 

<action name="queryh" class="cn.com.norm.web.action.QueryAction" method="queryByHttp">
<result type="json" />
</action>


如果我在具体执行的方法中加入下面代码,是可以正确获取到这个属性的。 
String getName = ServletActionContext.getActionMapping().getName();
System.out.println("-----getName----->>"+getName);

输出的就是上面配置文件中name的值:“ queryhp ” 。
但是我把上面这个代码写到过滤器中,就不行了,会报错。
package cn.com.faqm.tool;

import java.io.IOException;

import javax.servlet.*;

import org.apache.struts2.ServletActionContext;

public class OpFilter implements Filter {

        protected FilterConfig filterConfig;

        public void init(FilterConfig filterConfig) throws ServletException {
                this.filterConfig = filterConfig;
        }

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterchain) throws IOException, ServletException {
                if(servletRequest.getParameter("actId") != null && !servletRequest.getParameter("actId").equals("")){
                        String getName = ServletActionContext.getActionMapping().getName();
                        System.out.println("-----getName----->>"+getName);
                }
                
                        
                filterchain.doFilter(servletRequest, servletResponse);
        }

        public void destroy() {
                this.filterConfig = null;
        }
}

错误信息如下:
2012-12-12 15:00:19 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
        at org.apache.struts2.ServletActionContext.getActionMapping(ServletActionContext.java:85)
  &nb