一:其中最重要的就是filter功能.它使用户可以改变一个request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个request到达servlet之前预处理request,也可以在response离开servlet时处理response.换种说法,filter其实是一个“servlet chaining“(servlet 链)
二: Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互
Strtus2不是Servlet,是使用了Filter过滤器来作为控制器,使用了拦截器,组成拦截器栈,是对Filter的改善,封装,简化
Filter与Struts2代码比较
三: Struts2出现以前要拦截客户端请求,完成过滤
1: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>
2: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>filter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>FilterDispatcher</filter-name>
<filter-class>com.filter.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDispatcher</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
3:FilterDispatcher
package com.filter;
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.http.HttpServletRequest;
public class FilterDispatcher implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
String path=null;
//1:获取servletPath
String servletPath=req.getServletPath();
System.out.println(servletPath);
//2:判断servletPath,若等于"/product-input.action,则转发到WEB-INF/pages/input.jsp
if("/filter/product-input.action".equals(servletPath))
{
path="/WEB-INF/pages/input.jsp";
}
//3:若等于"/product-save.action,则
if("/filter/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(productName, productDesc, Double.parseDouble(productPrice));
//(3)执行保存操作
System.out.println("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);
}
@Override
public void destroy() {
}
}
4: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>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="post">
ProductName:<input type="text" name="productName"><br/>
ProductdDesc:<input type="text" name="productDesc"><br/>
ProductdPrice:<input type="text" name="productPrice"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
5: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>Insert title here</title>
</head>
<body>
ProductName:${requestScope.product.productName}<br/>
ProductDesc:${requestScope.product.productDesc}<br/>
ProductPrice:${requestScope.product.productPrice}<br/>
</body>
</html>
6: Product
package com.filter;
public class Product {
private String productName;
private String productDesc;
private double productPrice;
public Product(String productName, String productDesc, double productPrice) {
this.productName = productName;
this.productDesc = productDesc;
this.productPrice = 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 double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
@Override
public String toString() {
return "Product [productName=" + productName + ", productDesc="
+ productDesc + ", productPrice=" + productPrice + "]";
}
}
使用Filter作为过滤器,拦截客户端请求,代码有冗余,而且代码之间耦合性太高
四:Struts2出现了以后
1: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>
2:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置 Struts2 的 Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3:struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="controller" extends="struts-default" namespace="/">
<action name="product-input" class="cn.mldn.controller.ProductAction" method="input">
<result name="input" type="dispatcher">/pages/input.jsp</result>
</action>
<action name="product-save" class="cn.mldn.controller.ProductAction" method="save">
<result name="details" type="dispatcher">/pages/details.jsp</result>
</action>
</package>
</struts>
4:PersonAction
package cn.mldn.controller;
public class ProductAction {
private String productName;
private String productDesc;
private double 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 double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
@Override
public String toString() {
return "Product [productName=" + productName + ", productDesc="
+ productDesc + ", productPrice=" + productPrice + "]";
}
public String input()
{
System.out.println("input"+this);
return "input";
}
public String save()
{
System.out.println("save"+this);
return "details";
}
}
5: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>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="post">
ProductName:<input type="text" name="productName"><br/>
ProductdDesc:<input type="text" name="productDesc"><br/>
ProductdPrice:<input type="text" name="productPrice"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
6: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>Insert title here</title>
</head>
<body>
ProductName:${productName}<br/>
ProductDesc:${productDesc}<br/>
ProductPrice:${productPrice}<br/>
</body>
</html>
以上使用了Struts2后,代码点的比以前更加少了,灵活了,类之间耦合性降低了,非常好