[原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC

时间:2021-11-14 00:09:01

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.MVC 设计模式概览:

实现 MVC(Model、View、Controller) 模式的应用程序由 3 大部分构成:

   1)模型:封装应用程序的数据和业务逻辑:POJO(Plain Old Java Object)

   2)视图:实现应用程序的信息显示功能:JSP

   3)控制器:接收来自用户的输入,调用模型层,响应对应的视图组件:Servlet,Filter

2. 使用 Filter 作为控制器的好处 使用一个过滤器来作为控制器, 可以方便地在应用程序里对所有资源(包括静态资源)进行控制访问.:<url-pattern>*.action</url-pattern>

Servlet VS Filter

Servlet 能做的 Filter 是否都可以完成 ? 嗯。 Filter 能做的 Servlet 都可以完成吗 ? 拦截资源却不是 Servlet 所擅长的! Filter 中有一个 FilterChain,这个 API 在 Servlet 中没有!

3.demo

  

[原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC

[原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC

  

代码

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>Insert title here</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>input page</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>

detial.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>detail page</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>

product.java

 package com.jason.struts.helloword;

 public class Product {

     private Integer productId;
private String productName;
private String productDesc; private double productPrice; public Product() {
super();
} public Product(Integer productId, String productName, String productDesc,
double productPrice) {
super();
this.productId = productId;
this.productName = productName;
this.productDesc = productDesc;
this.productPrice = productPrice;
} @Override
public String toString() {
return "Product [productId=" + productId + ", productName="
+ productName + ", productDesc=" + productDesc
+ ", 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;
} }

FilterDispatcher.java

 package com.jason.struts.helloword;

 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;
import javax.servlet.http.HttpServletResponse; /**
* Servlet Filter implementation class FilterDispatcher
*/
@WebFilter("*.action")
public class FilterDispatcher implements Filter { public void destroy() { } 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);
String path = null;
//2.判断 servletPath,若其等于 "/product-input.action", 则转发到WEB-INF/struts/input.jsp
if("/struts2/product-input.action".equals(servletPath)){
path = "/struts2/input.jsp"; }
//3.若servletPath,为"/product-save.action",则
if("/struts2/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));
//3) 执行保存操作
System.out.println("save Product" + product + "" ); product.setProductId(1001);
request.setAttribute("product", product); path = "/struts2/details.jsp"; }
//4.把Product 对象保存到request 中。 if(path != null){
req.getRequestDispatcher(path).forward(request, response);
return;
} chain.doFilter(request, response);
} public void init(FilterConfig fConfig) throws ServletException { } }