(01)Struts2_使用 Filter 作为控制器的 MVC 应用

2018-09-19 12:58:29 浏览数 (1)

MVC 设计模式概览

实现 MVC(Model、View、Controller) 模式的应用程序由 3 大部分构成:
  • 模型:封装应用程序的数据和业务逻辑
  • 视图:实现应用程序的信息显示功能
  • 控制器:接收来自用户的输入,调用模型层,响应对应的视图组件(Servlet 、Filter)

需求:

编码:

index.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
    <a href="czy_input.action">进入Input.jsp页面</a>
</body>
</html>
Input.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <form action="czy_save.action" method="post">
       编号:<input type="text" name="dlh"/><br>
       姓名:<input type="text" name="name"/><br>
       部门:<input type="text" name="bmmc"/><br>
       <input type="submit" value="保存"/>
   </form>
  </body>
</html>
Show.jsp
代码语言:javascript复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
       ID:${requestScope.czy.id}<br>
       编号:${ requestScope.czy.dlh}<br>
       姓名:${ requestScope.czy.name}<br>
       部门:${ requestScope.czy.bmmc}<br>
  </body>
</html>
FilterDispatcher.java
代码语言:javascript复制
package com.qbz.struts2_01;

import java.io.IOException;
import java.util.UUID;

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;
import javax.servlet.http.HttpServletResponse;

public class FilterDispatcher implements Filter {

    @Override
    public void destroy() {

    }

    @SuppressWarnings("unused")
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String servletPath = request.getServletPath();
        String path = null;

        if("/czy_input.action".equals(servletPath)){
            path="/WEB-INF/page/Input.jsp";
        }else if("/czy_save.action".equals(servletPath)){
            String dlh = request.getParameter("dlh");
            String name = request.getParameter("name");
            String bmmc = request.getParameter("bmmc");

            String id = UUID.randomUUID().toString().replace("-", "");
            GG_CZY czy = new GG_CZY(id,dlh, name, bmmc);

            request.setAttribute("czy", czy);

            path = "/WEB-INF/page/Show.jsp";
        }

        if(path!=null){
            request.getRequestDispatcher(path).forward(request, response);
            return;
        }
        arg2.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}
GG_CZY.java
代码语言:javascript复制
package com.qbz.struts2_01;

public class GG_CZY {
    private String id;
    private String dlh;
    private String name;
    private String bmmc;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDlh() {
        return dlh;
    }

    public void setDlh(String dlh) {
        this.dlh = dlh;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBmmc() {
        return bmmc;
    }

    public void setBmmc(String bmmc) {
        this.bmmc = bmmc;
    }

    public GG_CZY(String id, String dlh, String name, String bmmc) {
        super();
        this.id = id;
        this.dlh = dlh;
        this.name = name;
        this.bmmc = bmmc;
    }

    public GG_CZY() {
        super();
    }

    @Override
    public String toString() {
        return "GG_CZY [id="   id   ", dlh="   dlh   ", name="   name
                  ", bmmc="   bmmc   "]";
    }

}
web.xml
代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>struts2-01</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 拦截所有后缀为.action的请求 -->
    <filter>
        <filter-name>filterDispatcher</filter-name>
        <filter-class>com.qbz.struts2_01.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filterDispatcher</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
</web-app>

使用 Filter 作为控制器的好处

使用一个过滤器来作为控制器, 可以方便地在应用程序里对所有资源(包括静态资源)进行控制访问.

Servlet VS Filter

  • Servlet可以做到的,Filter都可以完成。
  • 拦截资源却不是Servlet所擅长的。
  • Filter 中有一个FilterChain,Servlet中没有这个API。

0 人点赞