一、 MVC设计模式概览
实现 MVC(Model、View、Controller) 模式的应用程序由 3 大部分构成:
(1). 模型:封装应用程序的数据和业务逻辑,用普通Java类(Plain Old Java Object, 简称POJO)来实现业务逻辑;
(2). 视图:实现应用程序的信息显示功能,用JSP页面来实现信息显示;
(3). 控制器:接收用户请求/输入,调用模型层,相应对应的视图组件,用Servlet或Filter来实现。
注意:采用Filter来作为控制器,可以方便地在应用程序中对所有资源(包括静态资源)进行控制访问,即Servlet能做的Filter也可实现,但Filter的拦截功能是Servlet所不擅长的,其API中没有FilterChain!
二、需求与流程分析
需求分析:
(1). 用户发送请求Product_input.action;
(2). 服务器接收请求后,显示表单,用户输入表单信息(商品名称、描述、单价等)并提交;
(3). 发送Product_save.action请求,保存表单信息到数据库,并给出保存成功的提示和所保存商品的全部信息。
实现流程:采用FilterDispatcher来过滤请求,并进行相应的处理。
三、具体功能实现
1. 创建启动页面:在WebContent中新建index.jsp页面,并超链接到product-input.action
2. 创建拦截器Filter:对*.action请求进行拦截处理
/**
* Servlet Filter implementation class FilterDispatcher
*/
public class FilterDispatcher implements Filter {
public void destroy() { }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 1. 获取ServletPath
HttpServletRequest req = (HttpServletRequest) request;
String servletPath = req.getServletPath();
System.out.println(servletPath); // /product-input.action
String path = null;
// 2. 若ServletPath为“/product-input.action”,则转发到/WEB-INF/pages/input.jsp
if("/product-input.action".equals(servletPath)) {
path = "/WEB-INF/pages/input.jsp";
}
// 3. 若其为“/product-save.action”,则转发到WEB-INF/pages/detail.jsp
if("/product-save.action".equals(servletPath)) {
// 3.1 获取请求参数
String productName = request.getParameter("productName");
String productDesc = request.getParameter("productDesc");
String productPrice = request.getParameter("productPrice");
// 3.2 把请求信息封装为Product对象(注:Product类略)
Product product = new Product(null, productName, productDesc, Double.parseDouble(productPrice));
// 3.3 执行保存操作
System.out.println(product);
product.setProductId(10001);
// 3.4 把Product对象保存到request中,便于转发后显示
// ${param.productName} --> ${requestScore.product.productName}
request.setAttribute("product", product);
path = "/WEB-INF/pages/details.jsp";
}
// 2.3 开始转发
if(path != null) {
request.getRequestDispatcher(path).forward(request, response);
return; // 转发后方法结束
}
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException { }
}
3. 创建输入页面:在/WEB-INF/pages目录下新建input.jsp页面,实现表单的显示与提交
<body>
<form action="product-save.action" method="post">
<table>
<tr>请添加商品信息:</tr>
<tr>
<td>ProductName:</td>
<td><input type="text" name="productName"/></td>
</tr>
<tr>
<td>ProductDesc:</td>
<td><input type="text" name="productDesc"/></td>
</tr>
<tr>
<td>ProductPrice:</td>
<td><input type="text" name="productPrice"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
4. 创建输入页面:在/WEB-INF/pages目录下新建details.jsp页面,实现所保存商品的详细信息
<body>
The Product has been saved...<a href="index.jsp">Return</a>
<table>
<tr>
<td>ProductID:</td>
<td>${requestScope.product.productId }</td>
</tr>
<tr>
<td>ProductName:</td>
<td>${requestScope.product.productName }</td>
</tr>
<tr>
<td>ProductDesc:</td>
<td>${requestScope.product.productDesc }</td>
</tr>
<tr>
<td>ProductPrice:</td>
<td>${requestScope.product.productPrice }</td>
</tr>
</table>
</body>