首先用到了ServletContext,全局容器的概念,之前不知道哪里有用,现在用到,这里解析的是一个sysCode的TAG,用于下拉框等选项的时候自动显示要选的内容。大致思路是,利用前一篇所说到的ESB服务器,先去注册一个获取sysCode的服务,然后存进ServletContext,其他模块使用的时候(CodeTag)直接在容器里面获取即可.

放入ServletContext步骤
 
public void init ()throws ServletException {
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
Properties prop = new Properties();
InputStream in = null;
try {
//读取配置文件
in = DictionaryCacheListener.class.getClassLoader().getResourceAsStream("codeutil.properties");
prop.load(in);
String property = prop.getProperty("syscode");//获取配置
String ctx = prop.getProperty("ctx");
if (StringUtils.isEmpty(ctx)) {
ctx = servletContext.getContextPath();
}
servletContext.setAttribute("ctx", ctx);
Map map = new HashMap();
map.put("data", property);
AjaxPageResponse ajaxPageResponse = EsbUtil.call(ESBService.common.GETSYSCODE, map);
List list = ajaxPageResponse.getData();
String result = JSON.toJSONString(list.get(0));
ObjectMapper mapper = new ObjectMapper();
Map<String,List> data = mapper.readValue(result, Map.class);
servletContext.setAttribute("code", data);//放入code data
in.close();
} catch (Exception e) {
logger.error("获取系统编码出错",e);
}finally{
if ( null != in) {
try {
in.close();
} catch (IOException e) {
logger.error("资源关闭失败",e);
}
}
}
} }

写成tag需要的类文件CondeTag

public class CodeTag extends SimpleTagSupport{
//编码名称
private String sysCode;
//编码值 (非必填)
private String value;
/*
* 忽略不显示(编码值) 当该值不为空时 对应的编码字段不出现
*/
private String ignore;
/*
* id属性
*/
private String id;
/*
* name 属性
*/
private String name;
/*
* 下拉框是否必填
*/
private String required;
/*
* 标签类型
*/
private String type;
/*
* 是否不可编辑
*/
private String disabled;
/*
* 过滤器
*/
private String filter;
/*
* class 样式
*/
private String cssclass; /*
* bootstrap
*/
private String bootstrap; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getCssclass() {
return cssclass;
} public void setCssclass(String cssclass) {
this.cssclass = cssclass;
} public String getFilter() {
return filter;
} public void setFilter(String filter) {
this.filter = filter;
} public String getDisabled() {
return disabled;
} public void setDisabled(String disabled) {
this.disabled = disabled;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getRequired() {
return required;
} public void setRequired(String required) {
this.required = required;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getIgnore() {
return ignore;
} public void setIgnore(String ignore) {
this.ignore = ignore;
} public String getValue() {
return value;
} public void setValue(String value) {
this.value = value;
} public String getSysCode() {
return sysCode;
} public void setSysCode(String sysCode) {
this.sysCode = sysCode;
} public String getBootstrap() {
return bootstrap;
} public void setBootstrap(String bootstrap) {
this.bootstrap = bootstrap;
} @Override
public void doTag() throws JspException, IOException {
String json = null;
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
Object attribute = servletContext.getAttribute("code");//将servletContext里面的code取出来???
Map<String, String> map = (Map<String, String>)attribute;
this.sysCode=sysCode.toLowerCase();//避免出现大小写 统一处理
json = map.get(sysCode);//将key为syscode的取出来封装成一个json
List<Map<String, String>> list = (List) JSONArray.parseArray(json, new HashMap<String,String>().getClass());
JspWriter out = getJspContext().getOut();
if (!StringUtils.isEmpty(type)) {
//标签
StringBuilder stringBuilder = new StringBuilder();
if ("select".equals(type)) {
//下拉框
stringBuilder.append("<select name=").append('"').append(name).append('"');
//id
if (!StringUtils.isEmpty(id)) {
stringBuilder.append(" id ='").append(id+"'");
}
//class 样式
if (!StringUtils.isEmpty(cssclass)) {
stringBuilder.append(" class='").append(cssclass+"'");
}
//添加是否必填校验
if (CodeUtil.YESNO.YESNO_YES.equals(required)) {
stringBuilder.append(" lay-verify='required'");
//兼容bootstrap
stringBuilder.append(" datatype='*'");
}
//添加是否可以编辑
if (CodeUtil.YESNO.YESNO_YES.equals(disabled)) {
stringBuilder.append(" disabled='disabled'");
}
//添加过滤器方法
if (!StringUtils.isEmpty(filter)) {
stringBuilder.append(" lay-filter='").append(filter+"'");
}
stringBuilder.append(">");
stringBuilder.append("<option value=''>请选择</option>");
//List<Map<String, String>> list = (List) JSONArray .parseArray(json, new HashMap<String,String>().getClass());
for (int i = 0; i < list.size(); i++) {
boolean flag = false;
if (!StringUtils.isEmpty(ignore)) {
String[] strs = ignore.split(",");
for(String s:strs) {
if (s.equals(list.get(i).get("codevalue"))) {
flag=true;
}
}
}
if (flag) {
continue;//是过滤内容就不执行下面的代码而是重头来一遍
}
stringBuilder.append("<option value='").append(list.get(i).get("codevalue")).append("'");
if (list.get(i).get("codevalue").equals(value)) {
stringBuilder.append(" selected='selected'");
}
stringBuilder.append(">").append(list.get(i).get("codename")).append("</option>");
}
stringBuilder.append("<select>");
}else if ("radio".equals(type)) {
//单选框
//List<Map<String, String>> list = (List) JSONArray .parseArray(json, new HashMap<String,String>().getClass());
for (int i = 0; i < list.size(); i++) {
if (!StringUtils.isEmpty(ignore) && ignore.equals(list.get(i).get("codevalue"))) {
continue;
}
if (CodeUtil.YESNO.YESNO_YES.equals(disabled)) {
stringBuilder.append("<label for='").append(name+"_"+i).append("'>");
stringBuilder.append("<input type='radio' name='").append(name+"' ").append("value='").append(list.get(i).get("codevalue")+"'");
stringBuilder.append(" title='").append(list.get(i).get("codename")+"'").append(" disabled='disabled'");
stringBuilder.append(" id='").append(name+"_"+i+"' ");
}else {
stringBuilder.append("<label for='").append(name+"_"+i).append("'>");
stringBuilder.append("<input type='radio' name='").append(name+"' ").append("value='").append(list.get(i).get("codevalue")+"'");
stringBuilder.append(" title='").append(list.get(i).get("codename")+"'");
stringBuilder.append(" id='").append(name+"_"+i+"' ");
}
if (list.get(i).get("codevalue").equals(value)) {
stringBuilder.append(" checked=''");
}
//为避免空值提交 默认第一个选中
if (StringUtils.isEmpty(value) && i==0) {
stringBuilder.append("checked=''");
}
if (!StringUtils.isEmpty(filter)) {
stringBuilder.append(" lay-filter='").append(filter+"'");
}
if (!StringUtils.isEmpty(cssclass)) {
stringBuilder.append(" class='").append(cssclass+"'");
}
stringBuilder.append(" >");
if(CodeUtil.YESNO.YESNO_YES.equals(bootstrap)){
//添加单选框的名称(兼容bootstrap)
stringBuilder.append("<span>");
stringBuilder.append(list.get(i).get("codename"));
stringBuilder.append("</span>");
}
stringBuilder.append("</label>");
} }else if("checkbox".equals(type)){//复选框没有做诶
//复选框 }
out.write(stringBuilder.toString());
}else {
//返回列表数据字典
if (StringUtils.isEmpty(value)) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<script>");
stringBuilder.append(" var data_").append(sysCode).append("=");
stringBuilder.append(json);
stringBuilder.append(";");
stringBuilder.append("function ");
stringBuilder.append(sysCode);
stringBuilder.append("(value){");
stringBuilder.append("for( var i=0;i<data_").append(sysCode).append(".length;i++){");
stringBuilder.append("if(value==data_").append(sysCode).append("[i].codevalue){");
stringBuilder.append("return data_").append(sysCode).append("[i].codename;");
stringBuilder.append("}");
stringBuilder.append("}");
stringBuilder.append("}");
stringBuilder.append("</script>");
out.write(stringBuilder.toString());
}else {
/**
* 定义 str 为 "" 主要在于JspWriter.out() null时会报空指针异常
* JspWriter.print() 可以避免空指针 但是当为null 时会输出为'null'字符串
*/
String str = "";
//返回对应字符编码
//List<Map<String, String>> list = (List) JSONArray .parseArray(json, new HashMap<String,String>().getClass());
for (int i = 0; i < list.size(); i++) {
Map<String, String> map2 = list.get(i);
if (value.equals(map2.get("codevalue"))) {
if (!value.equals(ignore)) {
str = map2.get("codename");
}
}
}
out.write(str);
}
}
} }

JSP tag的封装

<?xml version="1.0" encoding="GBK"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>syscode</short-name>
<!-- 这个属性非常重要,它指定该标签库的 URI,相当于指定该标签库的唯一标识。如上粗体字代码所示,JSP 页面中使用标签库时就是根据该 URI 属性来定位标签库的 -->
<uri>http://www.ylzinfo.com/syscodelib</uri>
<!-- 定义第一个标签 -->
<tag>
<!-- 定义标签名 该标签库的名称,这个属性很重要,JSP 页面中就是根据该名称来使用此标签的-->
<name>sysCode</name>
<!-- 定义标签处理类 指定标签的处理类,毋庸置疑,这个属性非常重要,指定了标签由哪个 Java 类来处理 -->
<tag-class>com.ylzinfo.tag.CodeTag</tag-class>
<!-- body-content:这个属性也很重要,它指定标签体内容。该元素的值可以是如下几个:
tagdependent:指定标签处理类自己负责处理标签体。
empty:指定该标签只能作用空标签使用。
scriptless:指定该标签的标签体可以是静态 HTML 元素,表达式语言,但不允许出现 JSP 脚本。
JSP:指定该标签的标签体可以使用 JSP 脚本。 -->
<body-content>empty</body-content>
<attribute>
<description>编码名称</description>
<name>sysCode</name>
<required>true</required>
</attribute>
<!-- 值 -->
<attribute>
<description>编码值</description>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 忽略syscode codevalue 的值 过滤 不显示 -->
<attribute>
<description>忽略值</description>
<name>ignore</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 控件类型 下拉 单选 多选 -->
<attribute>
<description>控件类型下拉、单选、多选</description>
<name>type</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 控件id值 -->
<attribute>
<description>控件id</description>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 控件name值 -->
<attribute>
<description>控件name属性</description>
<name>name</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 是否必填 -->
<attribute>
<description>是否必填</description>
<name>required</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 是否为bootstrap(兼容layerui) -->
<attribute>
<description>是否bootstrap</description>
<name>bootstrap</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 是否只读不可编辑 -->
<attribute>
<description>是否不可编辑</description>
<name>disabled</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- 过滤器 -->
<attribute>
<name>filter</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<!-- class样式 -->
<attribute>
<description>控件class属性</description>
<name>cssclass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute> </tag>
</taglib>

一个简单的使用

 <syscode:sysCode sysCode="aac004"/>
<syscode:sysCode sysCode="bysjs_status"/>
<syscode:sysCode sysCode="EDULEVEL"/>
<!--jsp文件开头就写上,用于查看code,方便调试--> <syscode:sysCode sysCode="bysjs_status" type="select" name="status" id="status" cssclass="form-control selectpicker"/>
<!--具体使用-->

ylz框架外网之JSP 自定义TAG的更多相关文章

  1. Jsp 自定义tag标签

    1转自:https://blog.csdn.net/yusimiao/article/details/46835617 Jsp自定义tag标签 自定义tag标签的好处 程序员可以自定一些特定功能的标记 ...

  2. YLZ开发外网前端

    YLZ外网前端使用的是boottstrap和layer和validform等插件 表格是用boottstrap的tablle加上框架人员开发的ajaxpageresponse进行协调开发 弹窗使用的l ...

  3. JSP自定义tag控件标签

    JSP支持自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件.tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码. 按照 ...

  4. JSP自定义tag

    前端需要调用后端的配置,想起velocity-tools.然而jsp的话,目前只能想到tag和EL表达式了. Tag相当好写,jsp2.0提供了简化写法: 编写一个java类: public clas ...

  5. JSTL+EL表达式+JSP自定义框架案例

    不会框架不要紧,我带你自定义框架 前言:这标题说的有点大了,当一回标题党,之前在学JSP的时候提到了JSTL和EL表达式,由于一直钟情于Servlet,迟迟没有更新别的,这回算是跳出来了.这回放个大招 ...

  6. jsp如何自定义tag的标签库?

    虽然和上一次的使用自定义的tld标签简化jsp的繁琐操作的有点不同,但是目的也是一致的.自定义tag比较简单. 1.新建tag标签 在WEB-INF目录下新建一个tags的文件夹,是自定义tag标签的 ...

  7. 自定义标签 与 JSTL(JSP Standard Tag Library)

    1.自定义标签 [理解]     [1]简介            > 在JSP2.0以后,在jsp页面中不建议使用脚本片段<% %>和JSP表达式<%= %>     ...

  8. JSP自定义标签之Hello Costom tag小例子

    1.项目结构 2.实现自定义tag所需依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId ...

  9. 总想自己动动手系列·1·本地和外网(Liunx服务器上部署的web项目)按照自定义的报文格式进行交互(准备篇)

    一.准备工作 (1)有一台属于自己的云服务器,并成功部署和发布一个web项目(当然,本质上来说Java-Project也没问题),通过外网IP可以正常访问该web项目. 需要说明的是:任何web项目, ...

随机推荐

  1. Linux系统中文件定位与查找

    Linux系统中文件查找 关键词 文件查找 | find | locate 本文主要介绍有关文件查找的两个命令——find和locate,以及压缩打包的命令——compress, gzip,bzip2 ...

  2. 进程锁,队列,JoinableQueue

    内容梗概: 1.进程同步(锁) 2.队列(重点) 3.生产者消费者模式 4.JoinableQueue([maxsize]) 5.信号量(了解) 6.事件 1.进程同步(锁) 并发编程让我们能更加充分 ...

  3. python基础之列表以及切片等操作

    列表 定义: 能装对象的对象,列表能放大量的数据,各种类型,且列表内的数据是可以修改保存的,常用 [ ] 去表示,每一项数据之间用逗号隔开 1.列表的索引与切片 1.1 索引 与字符串的索引几乎一致, ...

  4. 『计算机视觉』Mask-RCNN_锚框生成

    Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...

  5. RabbitMQ、Redis、Memcache

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  6. express文件上传中间件Multer详解

    express文件上传中间件Multer详解 转载自:https://www.cnblogs.com/chengdabelief/p/6580874.html   Express默认并不处理HTTP请 ...

  7. redisObject

    typedef struct redisObject {    unsigned type:4;    unsigned encoding:4;    unsigned lru:REDIS_LRU_B ...

  8. oracle 11g完全安装教程(CentOS)

    oracle下载链接:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html oracle ...

  9. CAS5.3-下载安装

    cas版本:cas5.3 环境准备:1.JDK8:2.Apache Maven:3.Apache Tomcat:4.git 1.通过git工具(本文使用的是TortoiseGit,也可用IDE或者gi ...

  10. linux shell 编程参考

    #!/bin/bash my_fun() { echo "$#" } echo 'the number of parameter in "$@" is '$(m ...