struts2:使用Filter作为控制器的MVC
来源:互联网 发布:js websocket链接不了 编辑:程序博客网 时间:2024/05/23 02:24
使用Fliter作为控制器,可以方便的在应用程序里对所有资源包括静态资源进行控制访问
以下是一个用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.setAttribute("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 + "]"; }}
0 0
- struts2:使用Filter作为控制器的MVC
- Struts2-1 使用Filter作为控制器的MVC
- 1.struts2:使用Filter作为控制器的MVC
- 使用filter作为控制器的MVC
- 使用Filter作为控制器的MVC
- 10001---使用 Filter 作为控制器的 MVC
- Struts2_001_使用 Filter 作为控制器的 MVC
- Strut2_使用Filter作为控制器的MVC应用
- (01)Struts2_使用 Filter 作为控制器的 MVC 应用
- Struts2学习笔记(二):Struts2、Servlet和Filter作为控制器的实现与对比
- 使用 Servlet 作为控制器,标准MVC模式
- 使用 Servlet 作为控制器,标准MVC模式
- 【MVC】使用Servlet 作为控制器实现一个简单的登陆验证
- struts2使用Filter作为Control实现sevlet转发功能
- 使用Servlet作为控制器
- 使用servlet作为控制器
- 使用Servlet作为控制器
- Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法
- php 中= ,==,===之间的区别
- 安装xampp后apache不能启动解决方法
- 数据结构(C语言版)”栈与队列“章节迷宫求解问题的思路与实现
- android5.1BatteryService深入分析
- vim语法不高亮问题
- struts2:使用Filter作为控制器的MVC
- 可拖动排序的ListView
- SpringMVC中ModelAndView跳转错误的问题
- HDU 4586 Play the Dice(概率+期望)
- HDU_1233《还是畅通工程》
- SpringMVC ajax post 提交数据,报错406:Not Acceptable
- WPF利用多线程的后台线程
- lucene(二)索引的建立 Directory,lockFactory
- 公司拷贝不了文件到U盘,如何处理呢?