博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Spring MVC学习指南(第2版)》——2.4 模型2之Filter分发器
阅读量:7046 次
发布时间:2019-06-28

本文共 3425 字,大约阅读时间需要 11 分钟。

本节书摘来自异步社区《Spring MVC学习指南(第2版)》一书中的第2章,第2.4节,作者:【美】Paul Deck著,更多章节内容可以访问云栖社区“异步社区”公众号查看

2.4 模型2之Filter分发器

虽然servlet是模型2应用程序中最常见的控制器,但过滤器也可以充当控制器。但请注意,过滤器没有作为欢迎页面的权限。仅输入域名时不会调用过滤器分派器。Struts 2使用过滤器作为控制器,是因为该过滤器也用于提供静态内容。

下面的例子(appdesign2)是一个采用filter分发器的模型2应用,目录结构如图2.5所示。

screenshot

图2.5 appdesign2目录结构

JSP页面和Product类同appdesign1相同,但没有采用servlet作为控制器,而是使用了一个名为FilterDispatcher的过滤器(见清单2.7)。

清单2.7 DispatcherFilter类

package appdesign2.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import appdesign2.action.SaveProductAction; import appdesign2.form.ProductForm; import appdesign2.model.Product; import java.math.BigDecimal; @WebFilter(filterName = "DispatcherFilter",     urlPatterns = { "/*" })public class DispatcherFilter implements Filter {   @Override  public void init(FilterConfig filterConfig)       throws ServletException {   }   @Override  public void destroy() {  }   @Override  public void doFilter(ServletRequest request,       ServletResponse response, FilterChain filterChain)       throws IOException, ServletException {     HttpServletRequest req = (HttpServletRequest) request;     String uri = req.getRequestURI();    /*      * uri is in this form: /contextName/resourceName, for      * example /appdesign2/input-product. However, in the      * case of a default context, the context name is empty,      * and uri has this form /resourceName, e.g.:      * /input-product      */    // action processing    int lastIndex = uri.lastIndexOf("/");    String action = uri.substring(lastIndex + 1);     String dispatchUrl = null;    if ("input-product".equals(action)) {       // do nothing       dispatchUrl = "/jsp/ProductForm.jsp";    }else if("save-product".equals(action)) {      // create form      ProductForm productForm = new ProductForm();      // populate action properties      productForm.setName(request.getParameter("name"));      productForm.setDescription(          request.getParameter("description"));      productForm.setPrice(request.getParameter("price"));      // create model      Product product = new Product();      product.setName(productForm.getName());      product.setDescription(product.getDescription());      try {        product.setPrice(new BigDecimal(productForm.getPrice()));      } catch (NumberFormatException e) {      }      // execute action method      SaveProductAction saveProductAction =            new SaveProductAction();      saveProductAction.save(product);       // store model in a scope variable for the view      request.setAttribute("product", product);      dispatchUrl = "/jsp/ProductDetails.jsp";     }     // forward to a view    if (dispatchUrl != null) {       RequestDispatcher rd = request           .getRequestDispatcher(dispatchUrl);       rd.forward(request, response);    } else {      // let static contents pass       filterChain.doFilter(request, response);    }   }}

doFilter方法的内容同appdesign1中process方法。

由于过滤器的过滤目标是包括静态内容在内的所有网址,因此,若没有相应的action则需要调用filterChain.doFilter()。

} else {      // let static contents pass       filterChain.doFilter(request, response);    }

要测试应用,可以用浏览器访问如下URL:

http://localhost:8080/appdesign2/input-product

转载地址:http://hbuol.baihongyu.com/

你可能感兴趣的文章
七年之痒? 可七年也会有大奖
查看>>
完整商业闭环金融手游“九点半”上线
查看>>
倍儿爽!QQ轻聊版打造纯办公体验
查看>>
大数据全栈式开发语言 – Python
查看>>
Linux虚拟内存地址转化成物理内存地址
查看>>
Android动画之萌萌哒蜡烛吹蜡烛动画
查看>>
Linux驱动技术(五) _设备阻塞/非阻塞读写
查看>>
电视上做独立音箱,小米的野望
查看>>
大数据时代:媒体新任务和媒体人的新角色
查看>>
欧美完成数据保护总协定谈判
查看>>
Open Baton起个大早,赶个晚集
查看>>
IDC发布2017年中国医疗IT 十大预测——医疗信息化向人工智能跨进
查看>>
数据爆发式增长云计算如何将挑战转化为机遇
查看>>
Spark将机器学习与GPU加速机制纳入自身
查看>>
盘点:2016年度WiFi行业十大事件
查看>>
怎样判断一个人是否适合做数据分析?
查看>>
私有云成功规划的四项法则
查看>>
五大尴尬掣肘大数据
查看>>
英特尔马子雅:深度学习四大痛点与BigDL解决之道
查看>>
Linux基础命令介绍八:文本分析awk
查看>>