lyloverly 2010-08-25 13:29
浏览 236
已采纳

Java struts2 中过滤器不好用

WEB.XML

authFilter
jp.cofesta.framework.web.filter.AuthorityFilter


encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8



struts2
org.apache.struts2.dispatcher.FilterDispatcher

<filter-mapping>
  <filter-name>authFilter</filter-name>
  <url-pattern>*.action</url-pattern>
</filter-mapping>

<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>*.*</url-pattern>
</filter-mapping>


<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.jsp</url-pattern>
</filter-mapping>

过滤器 实现类

public class AuthorityFilter extends OncePerRequestFilter {

private String error = "/logout.action";

/**
 * error action setter method
 * 
 * @param error
 */
public void setError(String error) {
    this.error = error;
}

/**
 * filter
 * 
 * @param request
 * @param response
 * @param filterChain
 */
protected void doFilterInternal(HttpServletRequest request,
        HttpServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    String requestURI = request.getRequestURI();
    //String shortRequestURI = requestURI.replace(request.getContextPath(),"");
    //ServletActionContext sac = new ServletActionContext(null,null);
    ServletActionContext.setRequest(request);
    User user = SecurityUtil.getLoginUser();
    if (requestURI.endsWith(error)) {
        filterChain.doFilter(request, response);
    }else if (user == null && !requestURI.endsWith(Constants.LOGINACTION)
            && !requestURI.endsWith(Constants.WELCOMEACTION)) {
        String forwardPath = request.getContextPath() + error;
        (new HttpServletResponseWrapper(response))
                .sendRedirect(forwardPath);
    } else {
        filterChain.doFilter(request, response);
    }
}

}

错误信息如下:

2010-8-25 13:34:56 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.setRequest(ServletActionContext.java:103)
at cc.yilian.invoicing.common.filter.AuthorityFilter.doFilterInternal(AuthorityFilter.java:51)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

  • 写回答

5条回答 默认 最新

  • iteye_5246 2010-08-25 15:58
    关注

    package com.xfaccp.filter;

    import java.io.IOException;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;

    import com.xfaccp.bean.UserBean;

    public class CheckLoginFilter implements Filter {

    public void destroy() {
        // TODO Auto-generated method stub
    
    }
    
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)arg0;
        String url = request.getServletPath();
        HttpSession session = request.getSession();
        UserBean user = (UserBean)session.getAttribute("user");
        if(url.indexOf("LoginServlet") > 0)
        {
            arg2.doFilter(arg0, arg1);
        }
        else if(user == null || user.getId() < 1)
        {
            request.setAttribute("loginError", "请先登录!");
            request.getRequestDispatcher("/jsp/login.jsp").forward(arg0, arg1);
        }else
        {
            arg2.doFilter(arg0, arg1);
        }
    
    }
    
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值