使用Filter作为控制器的web项目

使用filter 作为控制器处理请求

(Struts2就是使用 Filter 作为入口。SpringMVC 使用的是Servlet)

项目结构:

FilterDispatcher:

package com.cwh.struts2.helloworld;

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;

/**
 * Servlet Filter implementation class FilterDispatcher
 */
public class FilterDispatcher implements Filter {

    public FilterDispatcher() {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request2 = (HttpServletRequest)request;
        //1.获取servletPath
        String servletPath = request2.getServletPath();
        System.out.println(servletPath);

        String path = null;
        //2.若等于product-input.action,则转发到/WEB-INF/pages/input.jsp
        if("/product-input.action".equals(servletPath)) {
            path = "/WEB-INF/pages/input.jsp";
        }

        //3.如果不是空,则
        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 = new Product(null, productName, productDesc, Double.parseDouble(productPrice));

            //3).执行保存操作 
            System.out.println("have save:"+ product);
            //模拟保存操作(可以用orm 框架来进行操作)
            product.setProductId(1001);

            //4).把对象保存到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 fConfig) throws ServletException {
    }

}

实体类:

package com.cwh.struts2.helloworld;

public class Product {

    private Integer productId;
    private String productName;
    private String productDesc;
    private double 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;
    }

    public Product(Integer productId, String productName, String productDesc, double productPrice) {
        super();
        this.productId = productId;
        this.productName = productName;
        this.productDesc = productDesc;
        this.productPrice = productPrice;
    }

    public Product() {

    }
    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc
                + ", productPrice=" + productPrice + "]";
    }
}

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" 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">
  <display-name>struts2-1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <display-name>FilterDispatcher</display-name>
    <filter-name>FilterDispatcher</filter-name>
    <filter-class>com.cwh.struts2.helloworld.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>FilterDispatcher</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<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=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <form action="product-save.action" method="post">
    <br>
        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"/>
        <br><br>
    </a>
</body>
</html>

details.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    ProductId: ${requestScope.product.productId}
    <br>
    ProductName: ${requestScope.product.productName}
    <br>
    ProductDesc: ${requestScope.product.productDesc}
    <br>
    ProductPrice: ${requestScope.product.productPrice}

</body>
</html>

测试:http://localhost:8080/struts2-1/index.jsp 一直往下点,然后输入提交数据。有以下输出(对象属性值此处测试乱填的)

转载于:https://my.oschina.net/u/3780366/blog/1858161

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值