MVC设计模式概览
实现MVC(Model,View,Controller)模式的应用程序由3大部分构成:
-模型:封装应用程序的数据和业务逻辑(POJO,Plain Old Java Object)
-视图,实现应用程序的信息显示功能(Jsp)
-控制器,接收来自用户的输入,调用模型层,,响应对应的视图组件Servlet,Filter。
下面看代码:
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">Product Input</a> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>struts2-1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
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"> 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"> <br><br> </form> </body> </html>
package logan.struts.study; 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; /** * Servlet Filter implementation class FilterDispatcher */ @WebFilter("*.action") public class FilterDispatcher implements Filter { /** * Default constructor. */ public FilterDispatcher() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; //1.获取servletPath String servletPath = req.getServletPath(); System.out.println(servletPath); //2.判断servletPath,若其等于"/product-input.action"则转发到 ///WEB-INF/pages/input.jsp String path = null; if("/product-input.action".equals(servletPath)){ path = "/WEB-INF/pages/input.jsp"; } if(path != null){ request.getRequestDispatcher(path).forward(request, response); return; } chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
在浏览器里面输入http://localhost:8080/struts2-1/product-input.action
可以看到
添加代码如下:
Product.java
package logan.struts.study; public class Product { private Integer productId; private String productName; private String productDesc; private double 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; } public Product(Integer productId, String productName, String productDesc, double productPrice) { super(); this.productId = productId; this.productName = productName; this.productDesc = productDesc; this.productPrice = productPrice; } public Product() { } @Override public String toString() { return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc + ", productPrice=" + productPrice + "]"; } }
package logan.struts.study; 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; /** * Servlet Filter implementation class FilterDispatcher */ @WebFilter("*.action") public class FilterDispatcher implements Filter { /** * Default constructor. */ public FilterDispatcher() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; //1.获取servletPath String servletPath = req.getServletPath(); System.out.println(servletPath); //2.判断servletPath,若其等于"/product-input.action"则转发到 ///WEB-INF/pages/input.jsp String path = null; if("/product-input.action".equals(servletPath)){ path = "/WEB-INF/pages/input.jsp"; } //3.若其等于"/product-save.action"则 if("/product-save.action".equals(servletPath)){ //1).获取请求参数 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)); product.setProductId(1001); //3).执行保存操作 System.out.println("Save Product" + product); //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); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
details.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> 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>
当提交表单时
使用Filter作为控制器的好处:
使用一个过滤器来作为控制器,可以方便的在应用程序里对所有资源(包括静态资源)进行控制访问。
<url-pattern>*.action</url-pattern>
Servlet和Filter哪个更好?
Servlet能做的Filter都能做。
Filter能做的Servlet不一定能做,例如拦截资源。