- 内容提要
- 作者简介
- 译者简介
- 前言
- HTTP
- Servlet 和 JSP
- 下载 Spring 或使用 STS 与 Maven/Gradle
- 手动下载 Spring
- 使用 STS 和 Maven/Gradle
- 下载 Spring 源码
- 本书内容简介
- 下载示例应用
- 第 1 章Spring 框架
- 第 2 章模型 2 和 MVC 模式
- 第 3 章Spring MVC 介绍
- 第 4 章基于注解的控制器
- 第 5 章数据绑定和表单标签库
- 第 6 章转换器和格式化
- 第 7 章验证器
- 第 8 章表达式语言
- 第 9 章JSTL
- 第 10 章国际化
- 第 11 章上传文件
- 第 12 章下载文件
- 第 13 章应用测试
- 附录 A Tomcat
- 附录 B Spring Tool Suite 和 Maven
- 附录 C Servlet
- 附录 D JavaServer Pages
- 附录 E 部署描述符
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
2.4 模型 2 之 Filter 分发器
虽然 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绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论