本文主要是介绍使用Filter作为控制器的MVC,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MVC设计模式1.模型:POJO普通Java类
2.视图:JSP
3.控制器:Servlet Filter
请求->FilterDispatcher->DAO
步骤:
1.创建一个动态的Java工程
Filter vs Servlet
1.Servlet能做的Filter都能做;
2.Filter能拦截资源,Servlet不擅长。
index.jsp
<body><a href="product-input.action">Product input</a></body>
input.jsp
<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>
details.jsp
<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>
Product.java
package com.atguigu.struts2.helloworld;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(){}
}
FilterDispatchar.java
//新建一个Filter的Java类public class FilterDispatcher implements Filter{public void destroy(){}public void doFilter(ServletRequest request, ServletResponse response, Filter){//1.获取ServletPathHttpServletRequest req = (HttpServletRequest)request;String servlerPath = req.getServletPath();System.out.println(servletPath);String path = null;//2.判断servletPath,若其等于"/product-input.action",则转发到WEB-INF/pages/input.jspif("/product-input.action".equals(servletPath)){path = "WEB-INF/pages/input.jsp";}//3.若其等于"/product-save.action",则if("/product-save.action".equals(servletPath)){//①获取请求参数String productName = request.getParameter("productName");String productDesc = request.getParameter("productDesc");String productPrice = request.getParameter("productPrice");//②把请求信息封装为一个Product对象Product product = new Product(null, productName, productDesc, Double.parseDouble(productPrice));//③执行保存操作system.out.println("Save Product: " + product);product.setProductId(1001);//④把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);}
}
这篇关于使用Filter作为控制器的MVC的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!