[原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC
2016-08-01 17:35
686 查看
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在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
代码
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 { } }
相关文章推荐
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
- [原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- 《Struts2框架 》学习之Struts2 的引用(使用filter作为控制器的MVC应用)
- [原创]java WEB学习笔记62:Struts2学习之路--表单标签:form,表单标签的属性,textfield, password, hidden,submit ,textarea ,checkbox ,list, listKey 和 listValue 属性,select ,optiongroup ,checkboxlist
- [原创]java WEB学习笔记70:Struts2 学习之路-- 输入验证,声明式验证,声明是验证原理
- [原创]java WEB学习笔记54:Struts2学习之路--- 编写Struts2 的第一个程序,HelloWord,简述 package ,action,result
- [原创]java WEB学习笔记72:Struts2 学习之路-- 文件的上传下载,及上传下载相关问题
- struts2:使用Filter作为控制器的MVC
- [原创]java WEB学习笔记60:Struts2学习之路--Actioin-声明式异常处理
- [原创]java WEB学习笔记54:Struts2学习之路---概述,环境的搭建
- [原创]java WEB学习笔记56:Struts2学习之路---Struts 版本的 登录 demo
- [原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器
- (01)Struts2_使用 Filter 作为控制器的 MVC 应用
- [原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓存,时间戳缓存
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
- struts2之使用Filter作为控制器的MVC
- [原创]java WEB学习笔记69:Struts2 学习之路-- 消息处理与国际化,概述,配置国际资源文件,访问国际化消息,通过超链接切换语言
- [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map
- [原创]java WEB学习笔记58:Struts2学习之路---Result 详解 type属性,通配符映射