来源:http://yu.an.jin.5.blog.163.com/blog/static/53155811201082410106993/
package org.apache.struts2.dispatcher.ng.filter;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.ng.PrepareOperations;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Handles both the preparation and execution phases of the Struts dispatching process. This filter is better to use
* when you don't have another filter that needs access to action context information, such as Sitemesh.
*/
public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
private PrepareOperations prepare;
private ExecuteOperations execute;
//初始化过滤器
public void init(FilterConfig filterConfig) throws ServletException {
InitOperations init = new InitOperations(); //初始化辅助对象,封装了初始化的一些操作
try {
FilterHostConfig config = new FilterHostConfig(filterConfig); //对filterConfig进行封装
init.initLogging(config); //通过config,初始化内部Struts的记录
Dispatcher dispatcher = init.initDispatcher(config); //通过config,创建并初始化dispatcher
init.initStaticContentLoader(config, dispatcher); //通过config和dispatcher,初始化与过滤器相关的静态内容加载器
prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher); //通过config和dispatcher,创建request被处理前的系列操作对象
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);//通过config和dispatcher,创建处理request的系列操作对象
} finally {
init.cleanup(); //清空ActionContext
}
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
prepare.createActionContext(request, response); //创建ACTIONCONTEXT,并初始化Theadlocal
prepare.assignDispatcherToThread(); //指派dispatcher给Theadlocal
prepare.setEncodingAndLocale(request, response); //设置request的编码和LOCAL
request = prepare.wrapRequest(request); //封装request
ActionMapping mapping = prepare.findActionMapping(request, response); //查找并选择创建ActionMapping
if (mapping == null) { //如果映射不存在
boolean handled = execute.executeStaticResourceRequest(request, response); //试图执行一个静态资源的请求
if (!handled) {
chain.doFilter(request, response);
}
} else { //如果存在映射
execute.executeAction(request, response, mapping); //执行action
}
} finally {
prepare.cleanupRequest(request); //清除request的Threadlocal
}
}
public void destroy() {
prepare.cleanupDispatcher();
}
}
分享到:
相关推荐
StrutsPrepareAndExecuteFilter源码剖析 深入解析了struts2的工作原理、是一个学习struts2的很好的资料
这个bug是由Struts2上传文件后return SUCCESS...at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:103) 可以完美解决此类struts2的出错问题!
最新版本的Struts2是2.16版本,最大变化是:2.0.6版本是使用FilterDispatcher类,而2.1.6版本使用StrutsPrepareAndExecuteFilter类,请看web.xml配置即可。 这是一个翻写例子,是用来体会Struts2.1.6的好例子。 目的...
新struts2+jQuery所需包.rar\ 以及老版struts2所需jar包 新Struts2与老版struts2的区别如下: 配置web.xml文件时过滤器配置...<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。
Struts2标签库提供了主题、模板支持,极大地简化了视图页面的编写,而且,struts2的主题、... <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 4.标签的使用 。。。
1.本demo严格按照MVC的分层模式采用myeclipse编写,可直接运行,服务器为tomcat,数据库为mysql。 2.数据库操作使用c3p0连接池和dbtuils组件,...当然需要在web.xml中配置核心filter—strutsPrepareAndExecuteFilter.
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 引用个具体类文件 --> </filter> <filter-mapping> <filter-name>struts2</filter-name> ...
严重: Exception starting filter struts2 java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name>...
1 在Struts1中,核心控制器是ActionServlet,它是一个Servlet。在Struts2中哪个类是核心控制器,它也是一个Servlet么?(StrutsPrepareAndExecuteFilter) 2 在web.xml文件中如何部署Struts2。
在web.xml的display-name标签与welcome-file-list标签中加入一下代码 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context....
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter <filter-name>struts2 <url-pattern>/* struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...
官网对struts2的2.5版本做了一些修改,包括dispatcher(org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter)等,详见https://struts.apache.org/docs/struts-23-to-25-migration.html。
关于StrutsPrepareAndExecuteFilter最新的过滤器,在Struts.xml定义常量无法解决post方式提交中文乱码问题。 关于Struts2各个版本、各种过滤器,用get方式提交都方法都无法解决的中文乱码问题。
struts2官网2.3版本的最后一个版本,能够...注意此版本的dispatcher是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。详见https://struts.apache.org/docs/struts-23-to-25-migration.html。
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter <filter-name>struts2 <url-pattern>/* <!-- Character Encoding filter --> <!-- Character Encoding ...
crm项目的架构 * 创建web工程 * 引入jar包 * mysql的驱动包 * hibernate需要的jar包 * spring需要的jar包 * struts2需要的jar包 * jstl 需要的jar包 * junit需要的jar包 * 包的介绍 ...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter ``` struts.xml 文件中,需要配置 Action 的映射关系,例如: ``` <param name="actionName">HelloWorld ...
<?xml version="1.0" encoding="UTF-8"?... org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter <!-- 拦截所有的url --> <filter-name>struts2 <url-pattern>/* </web-app>