从零写一个Java WEB框架(七)Controller层转换器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GG_and_DD/article/details/80862178
  • 该系列,其实是对《架构探险》这本书的实践。本人想记录自己的学习心得所写下的。
  • 从一个简单的Servlet项目开始起步。对每一层进行优化,然后形成一个轻量级的框架。
  • 每一篇,都是针对项目的不足点进行优化的。
  • 项目已放上github

本篇

上一篇已经为转换器准备了几个bean类,例如:ParamView,Data 类。本篇就真正的开始写转换器了。

什么是转换器呢?

我的理解就是:继承HttpServlet类,重写里面的init()方法Service 方法。
init()方法是建立Servlet容器的时候,会调用该方法进行初始化,我们将用这个方法来加载我们的Helper方法。
Service 方法,转换器的核心方法。因为,每当有请求,都会调用这个类来进行对请求头的处理,其实在HttpServlet类也是充当一个转发的角色,因为在Service会判断请求头的请求方法,然后选择调用的是doGet()还是doPost()方法。

代码实现

DispatcherServlet 转换器实现

        /*
*  请求转发器
* */

@WebServlet(urlPatterns = "/*",loadOnStartup = 0)
public class DispatcherServlet extends HttpServlet {
    private static Logger log = LoggerFactory.getLogger(DispatcherServlet.class);

    /*
    *  思路:
    *  1. 在初始化Servlet容器的时候,加载相关的Helper类,Jsp路径,静态资源路径.
    *  2. 从request获取到请求方法和路径
    *     -》根据请求方法和路径去Controller容器获取封装了类和方法的Handler对象
    *     -》从Handler对象里拿到类名,然后从Bean容器里获取到该类的实例
    *     -》从request获取参数,将所有参数存到Param对象里面
    *     -》从Handler对象拿到方法名,通过ReflectionUtil工具类调用方法(传入对象,方法名,参数)
    *     -》从调用方法返回的结果判断是View类型的就进行View处理,返回JSP页面,如果是Data类型的就返回JSON数据。
    *
    * */

    @Override
    public void init(ServletConfig config) throws ServletException {
        //初始化相关的Helper
        HelperLoader.init();
        //获取ServletContext 对象
        ServletContext servletContext = config.getServletContext();
        //注册JSP路径
        ServletRegistration jsp = servletContext.getServletRegistration("jsp");
        jsp.addMapping(ConfigHelper.getAppJspPath() + "*");
        //注册处理静态资源的默认Servlet
        ServletRegistration def = servletContext.getServletRegistration("default");
        def.addMapping(ConfigHelper.getAppAssetPath() + "*");

        log.info("Init() success");

    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取到请求方法和路径
        String method = req.getMethod().toLowerCase();
        //getPathInfo() 和getRequestURI() 去区别就是
        // getPathInfo 是从Servlet配置路径开始获取额外路径的。
        // getRequestURI 则是从端口号开始获取路径的
        String pathInfo = req.getPathInfo();
        //获取Handler对象
        Handler handler = ControllerHelper.getHandler(method, pathInfo);
        if (handler != null) {
            //获取类
            Class<?> controllerClass = handler.getControllerClass();
            //获取实例对象
            Object bean = BeanHelper.getBean(controllerClass);
            //从request获取参数
            Enumeration<String> parameterNames = req.getParameterNames();
            Map<String, Object> paramMap = new HashMap<>();
            while (parameterNames.hasMoreElements()) {
                String s = parameterNames.nextElement();
                String parameter = req.getParameter(s);
                paramMap.put(s, parameter);
            }
            //获取方法名
            Method actionMethod = handler.getActionMethod();
            Param param = new Param(paramMap);
            log.info("调用方法名: "+ actionMethod.getName());
            //调用方法
            Object result = ReflectionUtil.invokeMethod(bean, actionMethod, param);
            //判断是否是View
            if (result instanceof View) {
                View view = (View) result;
                String path = view.getPath();
                if (StringUtils.isNotEmpty(path)) {
                    //如果是/开头,则是重定向
                    if (path.startsWith("/")) {
                        resp.sendRedirect(req.getContextPath() + path);
                    } else {
                        //转发
                        Map<String, Object> map = view.getModel();
                        for (Map.Entry<String, Object> m : map.entrySet()) {
                            req.setAttribute(m.getKey(),m.getValue());
                            log.info("key "+ m.getKey());
                        }
                        req.getRequestDispatcher(ConfigHelper.getAppJspPath()+path).forward(req,resp);
                    }
                } else if (result instanceof Data) {
                    //返回JSON数据
                    Data data = (Data) result;
                    Object model = data.getModel();
                    if (model != null) {
                        resp.setContentType("application/json");
                        resp.setCharacterEncoding("utf-8");
                        PrintWriter writer = resp.getWriter();
                        String s = JsonUtil.toJson(model);
                        writer.write(s);
                        writer.flush();
                        writer.close();

                    }
                }
            }
        }
    }


}

CustomerController 层实现

@Controller
public class CustomerController {

    @Inject
    private CustomerService customerService;

    /*
     *  进入客户列表界面
     * */
    @Action("get:/customer")
    public View index(Param param) {
        List<Customer> customerList = customerService.getCustomerList();
        return new View("customer.jsp").addModel("customerList", customerList);
    }

    /*
     *  显示客户基本信息
     * */
    @Action("get:/customer_show")
    public View show(Param param) {
        long id = param.getLong("id");
        Customer customer = customerService.getCustomer(id);
        return new View("customer_show.jsp").addModel("customer", customer);
    }


    /*
     *  进入创建客户界面
     * */
    @Action("get:/customer_create")
    public View create(Param param) {
        return new View("customer_create.jsp");
    }

    /*
     *  处理创建客户请求
     * */
    @Action("post:/customer_create")
    public Data createSubmit(Param param) {
        Map<String, Object> map = param.getMap();
        boolean result = customerService.createCustomer(map);
        return new Data(result);
    }

    /*
     *  进入编辑客户 界面
     * */
    @Action("get:/customer_edit")
    public View edit(Param param) {
        long id = param.getLong("id");
        Customer customer = customerService.getCustomer(id);
        return new View("customer_edit.jsp").addModel("customer", customer);
    }

    /*
    *  处理编辑客户请求
    * */
    //TODO


    /*
    *  处理删除客户请求
    * */
    //TODO
}

效果图
image.png

总结

一个轻量级的框架就这样完成了。从Controller层到Service层,再到Dao层。我觉得框架的雏形已经是出现了,但是我觉得在这么轻便的框架,我们可以实现很多想法。
下一篇,就是讲框架代码和业务代码分离开。完成一个真正的框架。

猜你喜欢

转载自blog.csdn.net/GG_and_DD/article/details/80862178