Tag: struts2
Struts2项目中配置多个过滤器
2010-10-17, Nortan Posted in Java, 软件开发 | 7 回复 | 查看全文>>
Struts2中的项目中,都会在web.xml文件中配置org.apache.struts2.dispatcher.FilterDispatcher这个filter,使得Struts能起作用,然而如果项目中使用多个过滤器,则在web.xml中FilterDispatcher这个过滤器需要配置在所有filter的后面,防止你的过滤器被Struts的这个过滤器中断你的配置,造成你的Filter无法执行。
以前做项目没有太注意,一般情况下FilterDispatcher也是放在最后,自己总认为只要配置好了,一切只是执行顺序问题,但最近开始一个项目,把一个Filter放在了FilterDispatcher的后面,启动过程中显示Filter已经正常初始化,确总是没有执行。看了看Struts的源码,原来问题出在FilterDispatcher这个Filter上,如下是doFilter方法中的代码:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; ServletContext servletContext = getServletContext(); String timerKey = "FilterDispatcher_doFilter: "; try { UtilTimerStack.push(timerKey); request = prepareDispatcherAndWrapRequest(request, response); ActionMapping mapping; try { mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager()); } catch (Exception ex) { LOG.error("error getting ActionMapping", ex); dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex); return; } if (mapping == null) { String resourcePath = RequestUtils.getServletPath(request); if ("".equals(resourcePath) && null != request.getPathInfo()) { resourcePath = request.getPathInfo(); } if (serveStatic && resourcePath.startsWith("/struts")) { findStaticResource(resourcePath, findAndCheckResources(resourcePath), request, response); } else { chain.doFilter(request, response);//激活下一个过滤器 } // The framework did its job here return; } dispatcher.serviceAction(request, response, servletContext, mapping); } finally { try { ActionContextCleanUp.cleanUp(req); } finally { UtilTimerStack.pop(timerKey); } } }
看上面的代码 mapping=actionMapper.getMapping(request,dispatcher.getConfigurationManager()); 这个是得到当前请求Action的信息,比如Action的名字,命名空间,result值等,只要这个mapping不为null,过滤器就会直接执行action而不会激活下一个过滤器,那么什么时候才会激活下一个过滤器呢?答案要满足两个条件,这两个条件是: 查看全文…
java web开发框架组合所需要的jar包
2010-06-02, Nortan Posted in Java, 软件开发 | 3 回复 | 查看全文>>
现在使用java开发项目,比较流行的组合是struts + ibatis +spring ,这个组合现在大伙都用2.x版本,struts是非常好的web开发框架,而ibatis则负责对数据库的直接访问,spring负责业务层,这里并没有提到hibernate,不是因为它不强大,而是因为太强大,我们有时不太好驾驭它,用得不好,效率会非常的低,所以我决定放弃它.
如下给出的组合可能版本会有些变动,不过只要不是大版本升级,应该都可以匹配上,所以打不到想要的包时,相差不大都可以用. 查看全文…