Strut2_使用Filter作为控制器的MVC应用
2015-02-25 10:30
302 查看
在input.jsp页面输入表单信息,details.jsp中显示表单信息。Filter类进行逻辑处理,根据servletPath来执行不同的操作。
input.jsp:
details.jsp:
web.xml配置文件:
Servlet VS Filter
1.servlet 能做的Filter都能完成
2.Filter 独有FilterChain,这个API在Servlet中不存在,filter能进行拦截
input.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'input.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="product-save.action" method="post"> ProductName :<input type="text" name="productName"/><br> ProductDesc: <input type=" text" name="productDesc"/><br> ProductPrice: <input type=" text" name="productPrice"/><br> <input type="submit" value="Submit"/> </form> </body> </html>
details.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'details.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ProductName :${requestScope.product.productName} <br> ProductId :${requestScope.product.productId} <br> ProductDesc :${requestScope.product.productDesc} <br> ProductPrice :${requestScope.product.productPrice} <br> </body> </html>Filter类:
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.获取servlet的Path String serlvetPath=req.getServletPath(); System.out.println(serlvetPath); String path=null; //2.判断servletPath,若等于“product-input.action”则转发到input.jsp if("/product-input.action".equals(serlvetPath)){ path="/WEB-INF/pages/input.jsp"; } //3.若等于save则 if("/product-save.action".equals(serlvetPath)){ String productName= request.getParameter("productName"); String productDesc= request.getParameter("productDesc"); String productPrice= request.getParameter("productPrice"); Product product = new Product(null,productName,productDesc,Double.parseDouble(productPrice)); System.out.println("Save:"+ product.getProductName()); product.setProductId(Integer.valueOf(1001)); //将Product保存到 request中 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 arg0) throws ServletException { } }
web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <display-name>FilterDispatcher</display-name> <filter-name>FilterDispatcher</filter-name> <filter-class>com.hcx.struts2.helloWorld.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>FilterDispatcher</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
Servlet VS Filter
1.servlet 能做的Filter都能完成
2.Filter 独有FilterChain,这个API在Servlet中不存在,filter能进行拦截
相关文章推荐
- Struts2-1 使用Filter作为控制器的MVC
- 使用filter作为控制器的MVC
- [原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC
- 10001---使用 Filter 作为控制器的 MVC
- struts2:使用Filter作为控制器的MVC
- 使用Filter作为控制器的MVC
- (01)Struts2_使用 Filter 作为控制器的 MVC 应用
- Struts2_001_使用 Filter 作为控制器的 MVC
- struts2-(1)使用Filter作为控制器
- 1. 使用Filter 作为控制器
- 《Struts2框架 》学习之Struts2 的引用(使用filter作为控制器的MVC应用)
- struts2之使用Filter作为控制器的MVC
- 1.struts2:使用Filter作为控制器的MVC
- JSP使用Servlet作为控制器实现MVC模式实例详解
- Struts2学习笔记(二):Struts2、Servlet和Filter作为控制器的实现与对比
- 使用servlet作为控制器
- 详解Angularjs在控制器(controller.js)中使用过滤器($filter)格式化日期/时间实例
- 使用Servlet作为控制器
- JSP培训(6)——使用Servlet作为控制器实现MVC模式
- 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。