先来看看MVC设计模式
实现MVC(Model,View,Controller)模式的应用程序由三大部分构成:
--模型:封装应用程序的数据和业务逻辑:POJO(Plain Old Java Object),一个普通的java类
--视图:实现应用程序的信息显示功能 :jsp
--控制器:接收来自用户的输入,调用模型层,响应对应的视图组建: servlet 和 filter
Struts2的控制器是用Filter实现的
下面是用Filter来做controller的一个小实例
Product.java
package com.wul.strust2.helloworld; public class Product { private Integer productId; private String productName; private String productDesc; private double productPrice; public Product() { } public Product(Integer productId, String productName, String productDesc, double productPrice) { 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 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 double getProductPrice() { return productPrice; } public void setProductPrice(double productPrice) { this.productPrice = productPrice; } @Override public String toString() { return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc + ", productPrice=" + productPrice + "]"; } }
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8" /> <title>Hello</title> </head> <body> <a href="product-input.action">Product Input</a> </body> </html>
input.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8" /> <title>Hello</title> </head> <body> <form action="product-save.action" method="post"> ProductName:<input type="text" name="productName"/> <br><br> ProductDesc:<input type="text" name="productDesc"/> <br><br> ProductPrice:<input type="text" name="productPrice"/> <br><br> <input type="submit" value="Submit"/> </form> </body> </html>details.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8" /> <title>Hello</title> </head> <body> ProductId: ${requestScope.product.productId } <br><br> ProductName: ${requestScope.product.productName } <br><br> ProductDesc: ${requestScope.product.productDesc } <br><br> ProductPrice: ${requestScope.product.productPrice } <br><br> </body> </html>
package com.wul.strust2.helloworld; 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; import javax.servlet.http.HttpServletRequest; @WebFilter({ "/FilterDispatcher", "*.action" }) 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;//注意ServletRequest接口里是没有getServletPath方法的,其是HttpServletRequest子接口里的方法 String servletPath = req.getServletPath(); String path = null; //2.判断servletPath,若等于"/product-save.action",则转发到 //WEB-INF/pages/input.jsp if("/product-input.action".equals(servletPath)){ path = "/WEB-INF/pages/input.jsp"; } //3.若其等于"/product-save.action",则 //1).获取请求参数 if("/product-save.action".equals(servletPath)){ String productName = request.getParameter("productName"); String productDesc = request.getParameter("productDesc"); String productPrice = request.getParameter("productPrice"); //2).把请求信息封装成一个Product对象 Product product = new Product(null, productName, productDesc, Double.parseDouble(productPrice)); //3).执行保存操作 System.out.println("save product"+product.toString()); product.setProductId(1001);// 在这里就不进行保存操作了 //4).把Product对象保存到request中,${param.productName} -> ${requestScope.product.productName} request.setAttribute("product", product); path = "/WEB-INF/pages/details.jsp"; } if(path != null){ request.getRequestDispatcher(path).forward(request, response); return ; } chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { } }
由上面的实例可以明确Filter确实可以用来做controller
使用Filter作为控制器的好处
----使用一个过滤器作为控制器,可以方便的在应用程序里对所有资源(包括静态资源)进行控制访问。
<url-pattern>*.action</url-pattern>
Servlet vs Filter
1.Servlet能做的Filter是否都可以实现?嗯
2.Filter能做的Servlet都可以完成吗?拦截器资源却不是Servlet所擅长的。
Filter中有一个FilterChain,这个API在Servlet中没有
Filter的功能比servlet要强
文章参考:http://edu.51cto.com/course/course_id-960.html