6.自己动手写Java Web框架-Model

前一篇已经介绍了上下文,上下文中存储了一个全局的变量,在Controller调用其方法时,可以将绑定的参数用实际值替换,例如@RequestParam绑定的参数,就是获取request的参数值,用@PathVariable绑定的参数,就是获取requestUrl中对应位置的数值。本篇先介绍模型,这里的Model是Spring中jsp传参的Model,向其中放置键值对,然后在jsp中就可以直接用jstl获取使用了。

定义接口

新增接口jw-core/src/main/java/com/jw/ui/Model.java

package com.jw.ui;

import java.util.Collection;
import java.util.Map;

public interface Model {
    Model addAttribute(String attributeName, Object attributeValue);

    Model addAttribute(Object attributeValue);

    Model addAllAttributes(Collection<?> attributeValues);

    Model addAllAttributes(Map<String, ?> attributes);

    Model mergeAttributes(Map<String, ?> attributes);

    boolean containsAttribute(String attributeName);

    Map<String, Object> asMap();
}

实现接口

新增类jw-core/src/main/java/com/jw/ui/JwModel.java

package com.jw.ui;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class JwModel extends HashMap<String, Object> implements Model {

    public JwModel() {
    }

    public JwModel(String attributeName, Object attributeValue) {
        addAttribute(attributeName, attributeValue);
    }

    public JwModel(Object attributeValue) {
        addAttribute(attributeValue);
    }


    public JwModel addAttribute(String attributeName, Object attributeValue) {
        put(attributeName, attributeValue);
        return this;
    }

    @SuppressWarnings("rawtypes")
    public JwModel addAttribute(Object attributeValue) {
        if (attributeValue instanceof Collection && ((Collection) attributeValue).isEmpty()) {
            return this;
        }
        return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
    }

    public JwModel addAllAttributes(Collection<?> attributeValues) {
        if (attributeValues != null) {
            for (Object attributeValue : attributeValues) {
                addAttribute(attributeValue);
            }
        }
        return this;
    }

    public JwModel addAllAttributes(Map<String, ?> attributes) {
        if (attributes != null) {
            putAll(attributes);
        }
        return this;
    }

    public JwModel mergeAttributes(Map<String, ?> attributes) {
        if (attributes != null) {
            for (String key : attributes.keySet()) {
                if (!containsKey(key)) {
                    put(key, attributes.get(key));
                }
            }
        }
        return this;
    }

    public boolean containsAttribute(String attributeName) {
        return containsKey(attributeName);
    }

    public Map<String, Object> asMap() {
        return this;
    }

}

里面有一个特殊的方法addAttribute(Object attributeValue),给定一个数值就行,不要给定key。这里需要我们自动给它取一个key。如果是一个集合,就取集合中实际元素的类名,首字母小写;否则就取这个元素的类型,首字母小写。
新增类jw-core/src/main/java/com/jw/ui/Conventions.java

package com.jw.ui;

import java.util.Collection;

import com.jw.util.StringUtils;

public class Conventions {

    @SuppressWarnings("rawtypes")
    public static String getVariableName(Object attributeValue) {
        if (attributeValue instanceof Collection) {
            return StringUtils.lowerFirst(((Collection) attributeValue).iterator().next().getClass().getSimpleName());
        }
        return StringUtils.lowerFirst(attributeValue.getClass().getSimpleName());
    }
}

需要修改StringUtils,添加一个函数
修改jw-core/src/main/java/com/jw/util/StringUtils.java

/**
 * 首字母小写
 * 
 * @param str
 * @return
 */
public static String lowerFirst(String str) {
    if (StringUtils.isEmpty(str))
        return str;

    char ch = str.charAt(0);
    if (ch < 'A' || ch > 'Z')
        return str;

    char[] cs = str.toCharArray();
    cs[0] = Character.toLowerCase(ch);
    return String.valueOf(cs);
}

至此Model相关的定义结束。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值