科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道运用Filter,ThreadLocal和Reflection,实现form到bean的填充

运用Filter,ThreadLocal和Reflection,实现form到bean的填充

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

以Filter截取请求,填充至bean,并将此bean保存至ThreadLocal,在使用时直接从ThreadLocal中获取。

作者:Nurhachi Jang 来源:CSDN 2008年2月28日

关键字: java ThreadLocal Filter

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共4页)

 if(beanMethod != null) {
                    try {
                        beanMethod.invoke(beanObject, fieldValue);
                    } catch (IllegalArgumentException ex) {
                        request.setAttribute("MainErrMsg", "illegal argument for the method " + sb.toString() + " of " + beanName);
                        this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                        return;
                    } catch (InvocationTargetException ex) {
                        request.setAttribute("MainErrMsg", beanName + " invoke target exception ");
                        this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                        return;
                    } catch (IllegalAccessException ex) {
                        request.setAttribute("MainErrMsg", beanName + " illegal access to " + sb.toString());
                        this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                        return;
                    }
                }
            }
            Form2BeanUtil.put(beanObject);
           
            Method getAllErrors = null;
            try {
                getAllErrors = beanClazz.getDeclaredMethod("getAllErrors");
            } catch (SecurityException ex) {
                System.err.println("Security exception when call getAllErrors()");
            } catch (NoSuchMethodException ex) {
                System.err.println("No such method named getAllErrors()");
            }
           
            if(getAllErrors != null) {
                try {
                    isValid = ((HashMap)getAllErrors.invoke(beanObject)).isEmpty();
                } catch (IllegalArgumentException ex) {
                    System.err.println("Illegal argument for getAllErrors()");
                } catch (IllegalAccessException ex) {
                    System.err.println("Illegal Access to getAllErrors()");
                } catch (InvocationTargetException ex) {
                    System.err.println("Invocation target exception");
                }
            }
        }
       
        if(beanName == null || isValid) {
            chain.doFilter(request, response);
        } else {
            if(beanObject != null) {
                request.setAttribute("formbean", beanObject);
            }
            this.filterConfig.getServletContext().getRequestDispatcher(getRequestURI(request)).forward(request, response);
        }
    }

    /**
     * Destroy method for this filter
     *
     */
    public void destroy() {
    }

    public String getRequestURI(ServletRequest request) {
        String requestURI = ((HttpServletRequest)request).getRequestURI();
        String queryString = ((HttpServletRequest)request).getQueryString();
        return requestURI + queryString;
    }
   
    /**
     * Return a String representation of this object.
     */
    public String toString() {
       
        if (filterConfig == null) return ("SimpleFilter()");
        StringBuffer sb = new StringBuffer("SimpleFilter(");
        sb.append(filterConfig);
        sb.append(")");
        return (sb.toString());
    }

}
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章