1.数据库表结构
采用的是mysql数据库,先设计一下数据库系统字典表结构
CREATE TABLE `yp_sys_dictionary` (
`id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'id',
`pid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '父ID 分类下再分类',
`dictionary_type` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '数据类别',
`dictionary_value` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '数据名称',
`dictionary_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '数据库存值',
`dictionary_desc` varchar(400) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '数据描述',
`order_num` int(11) UNSIGNED NULL DEFAULT 1 COMMENT '顺序 ',
`status` int(1) NOT NULL DEFAULT 0 COMMENT '状态(0:正常 1:锁定)',
`del_flag` int(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记(0 未删除 1 已删除)',
`create_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建时间',
`update_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统字典表' ROW_FORMAT = Dynamic;
数据
2.Beetl自定义html标签
开始写标签
(1)自己写一个类继承org.beetl.core.GeneralVarTagBinding;
public class Dictionary extends GeneralVarTagBinding {
private final static Logger logger = LoggerFactory.getLogger(Dictionary.class);
@Override
public void render() {
Map atters = (Map) args[1];
String value = (String) atters.get("attr");//接收值
logger.debug(value);
//模拟数据库查询
List<SysDictionary> list = new ArrayList<>();
SysDictionary s1 = new SysDictionary();
s1.setDictionaryValue("男");
s1.setDictionaryCode("0");
list.add(s1);
SysDictionary s2 = new SysDictionary();
s2.setDictionaryValue("女");
s2.setDictionaryCode("1");
list.add(s2);
this.binds("<option value='"+list.get(0).getDictionaryCode()+"'>"+list.get(0).getDictionaryValue()+"</option>" +
"<option value='"+list.get(1).getDictionaryCode()+"'>"+list.get(1).getDictionaryValue()+"</option>");
this.doBodyRender();
}
}
(2)把你的类加入到beetl的配置文件
加入到beetl.properties.并起一个名字
#自定义标签 必须TAG开头
TAG.dictionary= com.yuanpeng.congfig.beetl.tag.Dictionary
然后注册这个组件(我是在BeetlConfig里配置的,这个java类是用来把beetl加入ioc容器里的,我是这么理解的也不知道对不对)
@Bean
public GroupTemplate getGroupTemplate(BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) throws IOException {
GroupTemplate gt = beetlGroupUtilConfiguration.getGroupTemplate();
Map<String,Object> shared = new HashMap<>();
//测试
shared.put("blogSiteTitle",templatesPath);
gt.setSharedVars(shared);
// 注册国家化函数
gt.registerFunction("i18n", new I18nFunction(wac));
//注册shiro
gt.registerFunctionPackage("so", new ShiroExt());
//注册自定义标签
gt.registerTag("dictionary", Dictionary.class);
return gt;
}
(3)最后一步前台标签
<select name="rolename" lay-filter="LAY-user-adminrole-type">
<#dictionary attr="asdf" ; value>
*[value]
</#dictionary>
</select>
<!--注:这个*[ ] 是我修改了这里
#指定变量占位符的开始符号 ${
DELIMITER_PLACEHOLDER_START=*[
#指定变量占位符的结束符号}
DELIMITER_PLACEHOLDER_END=]
-->