这是利用action模仿请求Servelet(单例)
作用:
1.减少web.xml代码量
2.将servelet中的代码转移到action中,只需要在action中定义业务逻辑则可。
1.定义一个过滤器
package com.struts2.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.HttpServletResponse; import com.struts2.action.Action; import com.struts2.action.HelloWorldAction; import com.struts2.utils.ServletUtils; public class DispatchFilter implements Filter { private HttpServletRequest request; private HttpServletResponse response; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2) throws IOException, ServletException { /** * 1.先获取HttpServletRequest,HttpServletResponse * 2.获取HelloWorldAction.action中的HelloWorldAction * 3.利用java的反射机制进行调用 */ this.request=(HttpServletRequest) request; this.response=(HttpServletResponse) response; String requestURI = this.request.getRequestURI(); System.out.println(requestURI); String actionName = ServletUtils.convert(requestURI); //利用java的反射机制调用该方法 try { //利用java反射机制创建一个新的HelloWorldAction对象 HelloWorldAction action = (HelloWorldAction) Class.forName("com.struts2.action."+actionName).newInstance(); String forward = action.execute(this.request, this.response); /** * 2种跳转方法 * *重定向 * request里的参数不起作用 * this.response.sendRedirect(jsp页面) * *转发 * 放在 request中的值可以取出来 * this.request.getRequestDispatcher("index.jsp").forward(this.request, this.response); * */ this.request.getRequestDispatcher("index.jsp").forward(this.request, this.response); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } @Override public void init(FilterConfig arg0) throws ServletException { } }
web.xml中配置为:
<filter> <filter-name>DispatchFilter</filter-name> <filter-class>com.struts2.filter.DispatchFilter</filter-class> </filter> <filter-mapping> <filter-name>DispatchFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
当接受到一个请求后
<a href="HelloWorldAction.action">请求一个action</a>
当过滤器接受到一个请求以后,进行过滤
1.先取出请求uri.
2.根据java反射机制取出action
package com.struts2.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorldAction implements Action { @Override public String execute(HttpServletRequest request, HttpServletResponse response) { System.out.println("Hello World Struts2"); return "index.jsp"; } }
3.取出action以后,就可以执行action中的方法
4.利用request/response转发
/** * 2种跳转方法 * *重定向 * request里的参数不起作用 * this.response.sendRedirect(jsp页面) * *转发 * 放在 request中的值可以取出来 * this.request.getRequestDispatcher("index.jsp").forward(this.request, this.response); * */ this.request.getRequestDispatcher("index.jsp").forward(this.request, this.response); // 这句话的意思是,上面返回了一个字符串"index.jsp",那么这里就是去寻找这个index.jsp,并将request和response放进去。