MVC设计模式的概览
实现MVC(Model -View- Controller(servlet/filter))
在index.jsp页面中有超链接跳转到input.jsp,在input.jsp中表单添加数据,提交并显示在details.jsp页面中
index.jsp页面中:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="product-input.action">input product</a>
</body>
</html>
input.jsp页面中
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="product-save.action"method="post"></form>
produceName:<input type="text" name="produceName">
produceDesc:<input type="text" name="produceDesc">
producePrice:<input type="text" name="producePrice">
<input type="submit" value="提交">
</body>
</html>
在details.jsp中
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
productId:${requestScope.product.productId}
productName:${requestScope.product.productName}
productDesc:${requestScope.product.productDesc}
productPrice:${requestScope.product.productPrice}
</body>
</html>
filter类中
package com.guigu.struts;
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.annotation.WebFilter;
/**
* Servlet Filter implementation class FilterDispatcher
*/
@WebFilter({ "/FilterDispatcher", "*.action" })
public class FilterDispatcher implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)request;
String servlePath=request.getServletPath();
String path=null;
//2.判断servlePath,若其等于"/product-input.action",则转发到"/WEB-INF/pages/input.jsp"
if("/product-input.action".equals(servlePath))
path="/WEB-INF/pages/input.jsp";
//3.若其等于/product-save.action,则
if("/product-save.action".equals(servlePath)) {
String productName=request.getParameter("productName");
String productDesc=request.getParameter("productDesc");
String productPrice=request.getParameter("productPrice");
//3.1获取请求信息封装为一个product对象
Product product=new Product (null,productName,productDesc, productPrice);
//3.2执行保存操作
System.out.println("保存操作");
product.setProductId(122);
//3.4把product对象保存在request中
request.setAttribute("product",product);
path="/WEB-INF/pages/details.jsp";}
if(path!=null)
{request.getRequestDispatcher(path).forword(request,response);}
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
product类
package com.guigu.struts;
public class Product {
private Integer productId;
private String productName;
private String productDesc;
private String productPrice;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductPrice() {
return productPrice;
}
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
public Product(Integer productId, String productName, String productDesc, String productPrice) {
super();
this.productId = productId;
this.productName = productName;
this.productDesc = productDesc;
this.productPrice = productPrice;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
}
使用Filter作为控制器好处:
使用一个过滤器作为控制器,可以方便应用程序中对所有资源(包括静态资源)进行控制访问
servlet 和filter 对比
1.servlet 能做的filter都能完成
2.filter一个有filterChain,这个API在servlet没有。