本文共 3425 字,大约阅读时间需要 11 分钟。
本节书摘来自异步社区《Spring MVC学习指南(第2版)》一书中的第2章,第2.4节,作者:【美】Paul Deck著,更多章节内容可以访问云栖社区“异步社区”公众号查看
虽然servlet是模型2应用程序中最常见的控制器,但过滤器也可以充当控制器。但请注意,过滤器没有作为欢迎页面的权限。仅输入域名时不会调用过滤器分派器。Struts 2使用过滤器作为控制器,是因为该过滤器也用于提供静态内容。
下面的例子(appdesign2)是一个采用filter分发器的模型2应用,目录结构如图2.5所示。
图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/