1. 程式人生 > >Servlet3.0實現的簡單mvc框架

Servlet3.0實現的簡單mvc框架

jar包準備:servlet-api-3.0.jar
專案結構如下:
這裡寫圖片描述

註解Mapping ,用於對映url地址

/*
 * 檔名:Action.java
 * 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述: Action.java
 * 修改人:peiyu
 * 修改時間:2016年7月29日
 * 修改內容:新增
 */
package com.servlet3.annotation;

import java.lang.annotation.ElementType;
import java.lang
.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Mapping { String value(); }

ActionModel,用於封裝Action資訊

/*
 * 檔名:ActionModel.java
 * 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述: ActionModel.java
 * 修改人:peiyu
 * 修改時間:2016年8月1日
 * 修改內容:新增
 */
package com.servlet3.util; import java.io.Serializable; import java.lang.reflect.Method; /** * @author peiyu */ public class ActionModel implements Serializable{ /** * 新增欄位註釋. */ private static final long serialVersionUID = 1L; private String className; private Method method; private
Object action; /** * 設定className. * @return 返回className */ public String getClassName() { return className; } /** * 獲取className. * @param className 要設定的className */ public void setClassName(String className) { this.className = className; } /** * 設定method. * @return 返回method */ public Method getMethod() { return method; } /** * 獲取method. * @param method 要設定的method */ public void setMethod(Method method) { this.method = method; } /** * 設定action. * @return 返回action */ public Object getAction() { return action; } /** * 獲取action. * @param action 要設定的action */ public void setAction(Object action) { this.action = action; } }

MyFilter過濾器,初始化時建立掃描指定包下的所有類的註解,解析URL,建立Bean

/*
 * 檔名:MyFilter.java
 * 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述: MyFilter.java
 * 修改人:peiyu
 * 修改時間:2016年7月29日
 * 修改內容:新增
 */
package com.servlet3.filter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.servlet3.annotation.Mapping;
import com.servlet3.exception.URLException;
import com.servlet3.util.ActionModel;
import com.servlet3.util.CommentedProperties;

/**
 * 
 * @author peiyu
 */
@WebFilter(filterName = "MyFilter", urlPatterns = {"/*"})
public class MyFilter implements Filter {
    @Override
    public void destroy() {
        System.out.println("-----------------------destroy-----------------------");
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String uri = httpServletRequest.getRequestURI();
        //訪問的邏輯地址/myServlet3
        String url = uri.substring(uri.indexOf("/", 1));
        httpServletRequest.getRequestURL();
        System.out.println("-----------------------doFilter-----------------------");
        request.setAttribute("url", url);
        request.getRequestDispatcher("/myServlet3").forward(request,response);

    }
    /**
     *1. 獲取配置檔案中配置的需掃描的包名
     *2. 根據包名掃描註解,獲取URL,建立Bean例項
     */
    @Override
    public void init(FilterConfig config) throws ServletException {
      //獲取配置檔案中的包路徑
        CommentedProperties properties = new CommentedProperties();
        //   /D:/software/apache-tomcat-7.0.59/webapps/Servlet3.0/WEB-INF/classes/scan-package.properties
        String classPath = MyFilter.class.getClassLoader().getResource("").getPath();
        String path = classPath + "scan-package.properties";
        System.out.println("####1載入配置檔案:" + path);
        try {
            path = URLDecoder.decode(path, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        System.out.println("####2載入配置檔案:" + path);
        try {
            properties.load(new FileInputStream(path), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //獲取到包路徑
        String scanPackage = properties.getProperty("scan-package").replace(".", "/");
        /*************************** 掃描包下的所有檔案 ***************************************/
        //獲取到class檔案路徑
        path = classPath + scanPackage;
        Map<String, ActionModel> urls = new HashMap<>();
        File file = new File(path);
        //獲取Action,url資訊
        getFileUrls(file, urls);
        //將Action,url資訊放到ServletContext
        config.getServletContext().setAttribute("urls", urls);
    }
    /**
     * 獲取URL.
     */
    private void getFileUrls(File file, Map<String, ActionModel> urls) {
        if (file.isDirectory()) { //如果是資料夾,獲取資料夾下的檔案
            File[] files = file.listFiles();
            for (File temp : files) {
                if (temp.isDirectory()) {
                    getFileUrls(temp, urls);
                } else {
                    getFileUrl(temp, urls);
                }
            }
        } else { //如果是檔案
            getFileUrl(file, urls);
        }
    }

    /**
     * 獲取URL,建立Action bean.
     */
    @SuppressWarnings("rawtypes")
    private void getFileUrl(File temp, Map<String, ActionModel> urls) {
        String path;
        path = temp.getPath();
        String className = path.substring(path.indexOf("com"), path.lastIndexOf(".")).replace("\\", ".");
        try {
            Class clazz = Class.forName(className);
            //獲取類註解
            String startUrl = "";
            //Annotation[] annotations = clazz.getAnnotations();
            //for (Annotation annotation : annotations) {
            //   if (annotation instanceof Mapping) { //判斷是mapping
            //       startUrl = ((Mapping) annotation).value();
            //    }
            //}
            if (clazz.isAnnotationPresent(Mapping.class)) {
                startUrl = ((Mapping)clazz.getAnnotation(Mapping.class)).value();
            }
            //獲取方法上註解
            Method[] methods = clazz.getMethods();
            Object action=clazz.newInstance();
            for (Method method : methods) {
                if (method.isAnnotationPresent(Mapping.class)) {
                    ActionModel actionModel = new ActionModel();
                    actionModel.setClassName(className);
                    actionModel.setMethod(method);
                    actionModel.setAction(action);
                    Mapping annotation=method.getAnnotation(Mapping.class);
                    String key = startUrl + ((Mapping) annotation).value();
                    if (urls.containsKey(key)) {
                        throw new URLException("已存在URL:" + key);
                    } else {
                        urls.put(key, actionModel);
                    }
                }
                //annotations = method.getAnnotations();
                //for (Annotation annotation : annotations) {
                //    if (annotation instanceof Mapping) { //判斷是mapping
                //        ActionModel actionModel=new ActionModel();
                //        actionModel.setClassName(className);
                //        actionModel.setMethod(method);
                //        actionModel.setAction(action);
                //        String key = startUrl + ((Mapping) annotation).value();
                //        if (urls.containsKey(key)) {
                //            throw new URLException("已存在URL:"+key);
                //        }else {
                //            urls.put(key, actionModel);
                //        }
                //    }
                //}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

scan-package.properties配置需要掃描的包

scan-package=com.servlet3.controller

預設訪問的Servlet,通過該Servlet找到指定的URL地址

/*
 * 檔名:MyServlet3.java
 * 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述: MyServlet3.java
 * 修改人:peiyu
 * 修改時間:2016年7月29日
 * 修改內容:新增
 */
package com.servlet3.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.servlet3.util.ActionModel;

/**
 * name:當前Servlet 的名稱
 * urlPatterns:請求的url
 * loadOnStartup:tomcat啟動時就初始化該servlet
 * initParams:初始化引數
 * name:引數名
 * value:引數值
 * 
 * @author peiyu
 */
@WebServlet(name = "MyServlet3", 
urlPatterns = {"/myServlet3"}, 
loadOnStartup = 1, 
initParams = {@WebInitParam(name = "name", value = "java"), 
                     @WebInitParam(name = "age", value = "1")})
public class MyServlet3 extends HttpServlet {
    /**
     * 新增欄位註釋.
     */
    private static final long serialVersionUID = 1L;
    @SuppressWarnings({"unchecked"})
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url=(String) req.getAttribute("url");
        Map<String, ActionModel> urls =(Map<String, ActionModel>) req.getServletContext().getAttribute("urls");
        ActionModel actionModel=urls.get(url);
        if (actionModel!=null) {
            try {
                Object res=actionModel.getMethod().invoke(actionModel.getAction(), req,resp);
                //根據返回的結果型別,返回值進行處理
                if (res instanceof String) {
                    if (((String) res).startsWith("redirect:")) {
                        //......
                    }else if (((String) res).startsWith("forward:")) {
                        //......
                    }
                }else{
                    //......
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}

測試的UserAction

/*
 * 檔名:UserAction.java
 * 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述: UserAction.java
 * 修改人:peiyu
 * 修改時間:2016年7月29日
 * 修改內容:新增
 */
package com.servlet3.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.servlet3.annotation.Mapping;

/**
 * @author     peiyu
 */
@Mapping(value="/myServlet3")
public class UserAction {

    @Mapping(value="/login")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        System.out.println("--------login-------------");
        return "redirect:register";
    }
    //register
    @Mapping(value="/register")
    public String register(HttpServletRequest req, HttpServletResponse resp){

        System.out.println("--------register-------------");
        return "redirect:login";
    }
}