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

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
    
    }
    

    }

    点赞 打赏 评论
  • iteye_5246 2010-08-25 14:32

    用Spring的过滤器
    <!-- Filter 定义 -->
    <!-- Character Encoding filter -->

    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter

    encoding
    UTF-8


    forceEncoding
    true

    点赞 打赏 评论
  • iteye_5246 2010-08-25 15:08

    哦,自定义过滤器验证用户登录状态
    一般处理是在登录过滤器中获取保存用户的session的用户当取不到用户时即NULL时跳转到登录页面
    上面的错误是表示你的AuthorityFilter.java:51行有错误是一个NullPointerException 异常

    点赞 打赏 评论
  • CoolSunfeng 2010-08-25 15:17

    你的代码有点晕,首先你是那什么来判断用户是否登录的,,都没看到页面传的参数。。。。

    点赞 打赏 评论
  • iteye_5246 2010-08-25 15:19

    你的那个SecurityUtil放的是获取session的吧
    建议使用
    HttpServletRequest request = ServletActionContext.getRequest();
    request.getSession().getAttribute("")的形式

    点赞 打赏 评论

相关推荐 更多相似问题