struts2:使用Filter作为控制器的MVC
2015-05-11 18:52
211 查看
使用Fliter作为控制器,可以方便的在应用程序里对所有资源包括静态资源进行控制访问
以下是一个用Filter作为控制器的例子
程序结构
web.xml的配置
index.jsp
input.jsp
details.jsp
FilterDispatcher.java
Product.java
以下是一个用Filter作为控制器的例子
程序结构
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <filter> <filter-name>product</filter-name> <filter-class>com.struts.first.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>product</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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/> ProductDesc:<input type="text" name="productDesc"/> <br/> ProductPrice:<input type="text" name="productPrice"/> <br/> <input type="submit" value="submit"/> </form> </body>
details.jsp
<body> ProductName:${requestScope.product.productId}<br/> ProductName:${requestScope.product.productName}<br/> ProductName:${requestScope.product.productDesc}<br/> ProductName:${requestScope.product.productPrice}<br/> </body>
FilterDispatcher.java
public class FilterDispatcher implements Filter { @Override public void destroy() {} @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("/product-input.action".equals(servletPath)){ path="/WEB-INF/pages/input.jsp"; } //3.若其等于"/product-input.action"则转发到/WEB-INF/pages/details.jsp if("/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.toString()); product.setProductId(1001); //4).把Product对象保存到request中.${param.productName}->${requestScope.product.productName} request.setA 4000 ttribute("product", product); path="/WEB-INF/pages/details.jsp"; } if(path!=null){ request.getRequestDispatcher(path).forward(request, response); return;//重要。转发完就代表已经给过响应了,该方法就要结束,就不能在执行后面的doFilter了 } chain.doFilter(request, response); } @Override public void init(FilterConfig config) throws ServletException {} }
Product.java
public class Product { private Integer productId; private String productName; private String productDesc; private Double productPrice; public Product(){} public Product(Integer productId, String productName, String productDesc, double productPrice) { super(); this.productId = productId; this.productName = productName; this.productDesc = productDesc; this.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; } @Override public String toString() { return "Product [productName=" + productName + ", productDesc=" + productDesc + ", productPrice=" + productPrice + "]"; } }
相关文章推荐
- Struts2-1 使用Filter作为控制器的MVC
- [原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC
- 1.struts2:使用Filter作为控制器的MVC
- struts2之使用Filter作为控制器的MVC
- 《Struts2框架 》学习之Struts2 的引用(使用filter作为控制器的MVC应用)
- (01)Struts2_使用 Filter 作为控制器的 MVC 应用
- Struts2_001_使用 Filter 作为控制器的 MVC
- 10001---使用 Filter 作为控制器的 MVC
- 使用filter作为控制器的MVC
- 使用Filter作为控制器的MVC
- struts2-(1)使用Filter作为控制器
- Struts2入门(一)_使用Filter实现MVC
- 1. 使用Filter 作为控制器
- Strut2_使用Filter作为控制器的MVC应用
- 【MVC】使用Servlet 作为控制器实现一个简单的登陆验证
- 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。
- Struts2学习笔记(二):Struts2、Servlet和Filter作为控制器的实现与对比
- 使用Servlet作为控制器的的MVC的应用-登录验证
- (asp.net mvc学习)ASP.NET MVC ActionFilter的学习与使用
- MVC使用jQuery从视图向控制器传递Model的2种方法