fromddd 2019-06-28
自定义MVC框架
一、    实现自定义MVC的体系结构图
1、Model I模式开发Web应用时,分两种情况:
  *纯JSP技术方式开发
  *JSP+JavaBean方式开发
2、Model I模式开发的不足:
  *JSP页面中嵌入大量的Java代码,可读性差。
  *大量代码在JSP中难以复用。
  *后期维护及扩展的难度大。
3、为了克服Model I模式的缺陷,引入了Model II的模式开发 
  *Model II模式体现了基于MVC(Model-View-Controller,模型-视图-控制器)的设计模式,简单的说,Model II模式就是将数据显示、流程控制和业务逻辑处理分离,使之相互独立。 
4、MVC设计模式由3个部分组成各部分的作用。
  *Model:模型,主要用于数据和业务的处理。
  *View:视图,用于数据显示。
  *Controller:控制器,用于流程控制。
5、MVC设计模式的特点
  *一个模型可以对应多个视图。
  *显示与逻辑控制分离。
  *分层控制,减低了代码间的耦合。
二、    我们如何创建一个自己的MVC框架??
(一)我们要在lib里面准备一个夹包
dom4j-1.6.1.jar   主要作用:解析xml文件
(二)准备配置文档(在src下)
<!DOCTYPE myframework[
<!ELEMENT myframework (actions) >
<!ELEMENT actions (action*)>
<!ELEMENT action (result*)>
<!ATTLIST action name CDATA #REQUIRED
                 class CDATA #REQUIRED
>
<!ATTLIST result name CDATA #IMPLIED
                redirect (true|false) "false"
>]>
解释:
解释:根据上述约束完成的“*”代表该节点可以出现多次
<myframework>
<actions>
        <action name="LoginAction" class="cn.action.LoginAction">
            <result name="success">success.jsp</result>
            <result name="login">login.jsp</result>
        </action>
    </actions></myframework>
(三)自己准备一个Action接口,用于放入结果集和执行方法
package cn.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Action {
public final String SUCCESS = "success";
public final String ERROR = "error";
public final String LOGIN = "login";
public final String INPUT = "input";
public String execute(HttpServletRequest request,
        HttpServletResponse response);}
(四)定义一个ResultMapping用来存放result节点
package cn.framework;
public class ResultMapping {
//result节点名字
private String name;
//是否重定向
private boolean redirect;
//跳转的页面
private String url;
public ResultMapping() {
}
public ResultMapping(String name, boolean redirect, String url) {
    this.name = name;
    this.redirect = redirect;
    this.url = url;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public boolean isRedirect() {
    return redirect;
}
public void setRedirect(boolean redirect) {
    this.redirect = redirect;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}}
(五)定义一个ActionMapping用来存放Action节点
package cn.framework;
import java.util.HashMap;
import java.util.Map;
public class ActionMapping {
// Action名称
private String name;
// Action名称对应的Action的类的全称
private String className;
// result集合
private Map<String, ResultMapping> results = new HashMap<String, ResultMapping>();
public ActionMapping() {
}
public ActionMapping(String name, String className, Map<String, ResultMapping> results) {
    super();
    this.name = name;
    this.className = className;
    this.results = results;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getClassName() {
    return className;
}
public void setClassName(String className) {
    this.className = className;
}
public Map<String, ResultMapping> getResults() {
    return results;
}
public void setResults(Map<String, ResultMapping> results) {
    this.results = results;
}}
(六)准备一个ActionMappingManager是用来管理ActionMapping的
package cn.framework;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ActionMappingManager {
Map<String, ActionMapping> actionMapping = new HashMap<String, ActionMapping>();
public ActionMappingManager() {
    init();
}
public ActionMapping getActionMapping(String actionName) {
    return actionMapping.get(actionName);
}
public ActionMappingManager(String fileName) {
}
public Map<String, ActionMapping> getActionMapping() {
    return actionMapping;
}
public void setActionMapping(Map<String, ActionMapping> actionMapping) {
    this.actionMapping = actionMapping;
}
public static void main(String[] args) {
    new ActionMappingManager().init();
}
public void init() {
    InputStream is = this.getClass().getResourceAsStream("/myframework.xml");
 
    SAXReader sr = new SAXReader();     
    try {
        Document doc = sr.read(is);
        Element elRoot = doc.getRootElement();
        List<Element> listActions = elRoot.elements();
        for (Element elActions : listActions) {
            List<Element> listAction = elActions.elements();
            for (Element elAction : listAction) {
                ActionMapping aMapping = new ActionMapping();
                Attribute attName = elAction.attribute("name");
                Attribute attClass = elAction.attribute("class");
                aMapping.setName(attName.getValue());
                aMapping.setClassName(attClass.getValue());
                List<Element> listResult = elAction.elements();
                for (Element elResult : listResult) {
                    ResultMapping rMapping = new ResultMapping();
                    Attribute attResultName = elResult.attribute("name");
                    Attribute attResultRedirect = elResult.attribute("redirect");
                    rMapping.setName(attResultName.getValue());
                    rMapping.setRedirect(Boolean.parseBoolean(attResultRedirect.getValue()));
                    rMapping.setUrl(elResult.getTextTrim());
                    aMapping.getResults().put(rMapping.getName(), rMapping);
                }
                actionMapping.put(aMapping.getName(), aMapping);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}}
(七)利用反射机制找到自己的实列
package cn.framework;
public class ActionManager {
public static Action createAction(String className) {
    Class<?> clz = null;
    try {
        clz = Thread.currentThread().getContextClassLoader().loadClass(className);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        if (clz == null) {
            clz = Class.forName(className);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    Action action = null;
    try {
        action = (Action) clz.newInstance();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return action;
}}
(八)写一个业务逻辑
package cn.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.framework.Action;
public class LoginAction implements Action {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) {
    return "success";
}}
(九)核心控制器Servlet
package cn.framework;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MVCServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
}
ActionMappingManager amanager = null;
@Override
public void init(ServletConfig config) throws ServletException {
    amanager = new ActionMappingManager();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ActionMapping am = amanager.getActionMapping(getActionName(request));
    Action action = ActionManager.createAction(am.getClassName());
    String r = action.execute(request, response);
    ResultMapping rm = am.getResults().get(r);
    if (rm.isRedirect()) {
        response.sendRedirect(rm.getUrl());
    } else {
        request.getRequestDispatcher(rm.getUrl()).forward(request, response);
    }
}
public String getActionName(HttpServletRequest request) {
    String actionName = null;
    String uri = request.getRequestURI();
    String contentPath = request.getContextPath();
    String actionPath = uri.substring(contentPath.length());
    actionName = actionPath.substring(1, actionPath.indexOf(".")).trim();
    return actionName;
}}
(十)修改web.xml
<servlet>
    <servlet-name>MVCServlet</servlet-name>
    <servlet-class>cn.framework.MVCServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MVCServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>(十一)准备一个login.jsp页面
(十二)发布到Tomcat运行