JAVA语言WEB框架的Controller层优化实例讲解
小标 2018-10-12 来源 : 阅读 1994 评论 0

摘要:本文主要向大家介绍了JAVA语言WEB框架的Controller层优化实例讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言WEB框架的Controller层优化实例讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。


本篇


前几篇的优化都没有涉及到Controller层。本篇开始将开始实现对Controller层的优化。由于上篇的IOC,创建了类容器。基于这个类容器,就可以很轻松地完成对Controller的优化了。


为了Controller解耦,还是决定使用请求容器(存放请求路径与处理方法映射关系),建立一个转换器。根据获取HttpRequest中的请求路径和参数,从请求容器里面去获取到相对应的方法。达到一个解耦的目的。


本篇主要实现功能:


- 封装请求信息


- 封装类和方法信息


- 将请求信息和方法信息形成K-V映射关系存到Map容器中


Controller 层实现目的:


根据Action注解实现请求路径和方法的绑定。


@Controller

public class CustomerServlet {

 

 

//    获取所有用户信息

    @Action("get:/customer_show/getList")

    public String getList() {

        //TODO

        return null;

    }

 

 

//    根据id获取用户信息

    @Action("get:/customer_show/getCustomer")

    public String getList(Integer id) {

        //TODO

        return null;

    }

}

   


功能实现


Request类 封装请求信息


/*

*

*   封装请求信息

*

*   思路:

*   1. 封装请求的信息, 正常的请求信息是: get:/customer_show

*      需要拆分成 requestMethod = get

*               requestPath  = /customer_show

*

*   2. 因为需要将请求信息放在Map中,以Request的对象为key,因为是对象作为key,

*       所以重写hashcode()和equals()方法,主要改变Map判断key的时候发挥作用。

* */

public class Request {

 

    /*

     *  请求方法

     * */

    private String requestMethod;

 

    /*

     *  请求路径

     * */

    private String requestPath;

 

    public Request(String requestMethod, String requestPath) {

        this.requestMethod = requestMethod;

        this.requestPath = requestPath;

    }

 

    public String getRequestMethod() {

        return requestMethod;

    }

 

    public String getRequestPath() {

        return requestPath;

    }

 

    @Override

    public boolean equals(Object o) {

        if (o instanceof Request) {

            Request r = (Request) o;

            //进行判断

            if (r.getRequestMethod().equals(this.requestMethod) &&

                    r.getRequestPath().equals(this.requestPath)) {

                return true;

            }

        }

 

        return false;

 

    }

 

 

    // 主要思路就是利用两个变量的hashCode,而不是对象的hashCode。

 

    @Override

    public int hashCode() {

        return Objects.hash(this.requestPath, this.requestMethod);

    }

}

   



 

Handler 类 封装Action注解的类和方法


/*

*  封装Action信息

*  封装类 和方法

* */

public class Handler {

 

    /*

    *  Controller注解 类

    * */

    private Class<!--?--> controllerClass;

 

    /*

     *  Action注解的 方法

     * */

    private Method actionMethod;

 

    public Handler(Class<!--?--> controllerClass, Method actionMethod) {

        this.controllerClass = controllerClass;

        this.actionMethod = actionMethod;

    }

 

    public Class<!--?--> getControllerClass() {

        return controllerClass;

    }

 

    public Method getActionMethod() {

        return actionMethod;

    }

}

   



 

ControllerHelper 类 建立有个Map容器用来存放request和handler的映射关系


/*

 *  控制类助手类

 * */

public class ControllerHelper {

    /*

     *  思路:

     *  1、遍历所有Controller 注解的类

     *  2、遍历每个类中的方法,找出有Action注解的方法

     *  3、从Action 注解中获取URL映射

     *  4、将URL映射存到Request对象,将该类和方法存到Handler对象

     *  5、存到Map集合中。 key---Request  value---Handler

     * */

 

    /*

     *  定义一个Map容器,存放Request和Handler的映射关系

     * */

    private final static Map<request, handler=""> ACTION_MAP =

            new HashMap<>();

 

    static{

        //获取所有具有Controoler注解的类

        Set<class<?>> controllerClassSet = ClassHelper.getControllerClassSet();

        if (CollectionUtils.isNotEmpty(controllerClassSet)) {

            //遍历类

            for (Class<!--?--> c : controllerClassSet) {

                //获取该类下的所有方法

                Method[] declaredMethods = c.getDeclaredMethods();

                if (declaredMethods != null) {

                    //遍历方法

                    for (Method m : declaredMethods) {

                        if (m.isAnnotationPresent(Action.class)) {

                            Action action = m.getAnnotation(Action.class);

      &n    

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved