JSP自定义select标签 字典表数据
开发过程中经常会重复用到select 加载字典表中的数据,比如加载职务、学历等等这些,每次用到都进行单独获取然后在前端进行遍历的话会很麻烦,而且不符合开发原则。因此可以通过自定义标签的方式进行封装。举例 如下:
ynbytag.tld
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"><description>YNBY 统一自定义标签</description><display-name>YNBY core</display-name><tlib-version>1.1</tlib-version><short-name>ynby</short-name><tag><name>demo</name><tag-class>com.rdpm.sysm.service.taglib.DemoTag</tag-class><body-content>empty</body-content></tag><tag><!--定义属性的名称。每个标签的是属性名称必须是唯一的。--> <name>selectTag</name><tag-class>com.rdpm.sysm.service.taglib.DictSelectTag</tag-class><body-content>empty</body-content><!--系统编码--> <attribute><name>systemCode</name><!--指定属性是否是必须的或者可选的,如果设置为false为可选。--> <required>true</required><!--声明在运行表达式时,标签属性是否有效。--> <rtexprvalue>true</rtexprvalue></attribute><!--select 的name值--> <attribute><name>selectName</name><!--指定属性是否是必须的或者可选的,如果设置为false为可选。--> <required>true</required><!--声明在运行表达式时,标签属性是否有效。--> <rtexprvalue>true</rtexprvalue></attribute><!--value 选中的值--> <attribute><name>value</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--默认选中的值--> <attribute><name>selectedValue</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--选中任意一项时触发的方法--> <attribute><name>onChange</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--选择方式 单选/多选--> <attribute><name>multiple</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--select 未选择时显示内容 如:"请选择"或者"所有"等--> <attribute><name>nullName</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--select 未选择时显示内容的位置 在顶部还是在底部--> <attribute><name>nullNamePlace</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--是否有下一级--> <attribute><name>hasNext</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--上级ID--> <attribute><name>parentId</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--额外的一些显示层的参数--> <!--样式类--> <attribute><name>cssClass</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute><!--内联样式--> <attribute><name>styleClass</name><required>false</required><rtexprvalue>true</rtexprvalue></attribute></tag> </taglib>
DictSelectTag.java
package com.rdpm.sysm.service.taglib;import com.rdpm.sysm.dao.BaseParameterMapper; import com.rdpm.sysm.entity.BaseParameter; import com.rdpm.sysm.util.SpringContextUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service;import javax.annotation.Resource; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import java.io.IOException; import java.util.List;/** * Created by XJM on 2016-07-12. */ public class DictSelectTag extends TagSupport {//额外的一些显示层的参数 private String cssClass;//样式类 private String styleClass;//内联样式 private String systemCode;//参数系统编码 private String selectName;//select 的name值 private String nullName;//未选择时给的提示信息选项 private String value;//选中的值 private String selectedValue;//选中的值 private String onChange;//改变时调用的方法 private String multiple;//选择方式 多选/单选 @Override public int doEndTag() throws JspException {
//SpringContextUtil 在最下面 BaseParameterMapper baseParameterMapper= (BaseParameterMapper) SpringContextUtil.getBean("baseParameterMapper");List<BaseParameter> list= baseParameterMapper.selectParamByParamTypeCode(systemCode);StringBuffer sb = new StringBuffer();JspWriter out = pageContext.getOut();sb.append("<select name=\""+this.getSelectName()+"\"");if (!StringUtils.isEmpty(this.getCssClass())){sb.append("class=\"" + this.getCssClass() + "\"");}if(!StringUtils.isEmpty(this.getStyleClass())){sb.append("style=\"" + this.getStyleClass() + "\"");}if(!StringUtils.isEmpty(this.getMultiple())){sb.append("multiple=\"" + this.getMultiple() + "\"");}if(!StringUtils.isEmpty(this.getOnChange())){sb.append("οnchange=\"" + this.getOnChange() + "\"");}sb.append(">");if(!StringUtils.isEmpty(this.getNullName())){sb.append("<option value=\"\">--"+this.getNullName()+"--</option>");}for(BaseParameter dc:list){if (dc.getDictValue().equals(this.getSelectedValue())){sb.append("<option value=\""+dc.getDictValue()+"\" selected>");}else {sb.append("<option value=\""+dc.getDictValue()+"\">");}sb.append(dc.getDictName()+"</option>");}sb.append("</select>");try {out.write(sb.toString());} catch (IOException e) {// TODO Auto-generated catch block throw new JspException(e);}return TagSupport.EVAL_PAGE;}public String getCssClass() {return cssClass;}public void setCssClass(String cssClass) {this.cssClass = cssClass;}public String getStyleClass() {return styleClass;}public void setStyleClass(String styleClass) {this.styleClass = styleClass;}public String getSystemCode() {return systemCode;}public void setSystemCode(String systemCode) {this.systemCode = systemCode;}public String getSelectName() {return selectName;}public void setSelectName(String selectName) {this.selectName = selectName;}public String getNullName() {return nullName;}public void setNullName(String nullName) {this.nullName = nullName;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}public String getOnChange() {return onChange;}public void setOnChange(String onChange) {this.onChange = onChange;}public String getSelectedValue() {return selectedValue;}public void setSelectedValue(String selectedValue) {this.selectedValue = selectedValue;}public String getMultiple() {return multiple;}public void setMultiple(String multiple) {this.multiple = multiple;} }
JSP
<%-- Created by IntelliJ IDEA. User: XJM Date: 2016-07-12 Time: 15:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="/WEB-INF/tld/ynbytag.tld" prefix="ynby"%> <html> <head><title>JSP自定义标签——Select 字典表数据</title> </head> <body> <ynby:selectTag systemCode="88" selectName="dictName" nullName="赶紧选择啊,亲" styleClass="color:red;width:200px" onChange="selectFunction()"></ynby:selectTag> <span id="nextSelect"></span> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script>function selectFunction() {var tmp='<ynby:selectTag systemCode="66" selectName="dictName" nullName="麻溜的"></ynby:selectTag>';$("#nextSelect").html($(tmp));} </script> </body> </html>
PS:该demo是在Spring MVC+Mybatis框架下写的,当需要使用DAO层时通过注入的方式是不可以,原因是能够注入的前提的是注入和被注入的类的对象都是Spring创建的,但自定义标签关联的类不是通过spring生成的对象。那么我想用怎么办呢?如下:
package com.rdpm.sysm.util;import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component;/** * Created by XJM on 2016-07-12. */ @Component public class SpringContextUtil implements ApplicationContextAware{private static ApplicationContext applicationContext;@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringContextUtil.applicationContext=applicationContext;}public static ApplicationContext getApplicationContext(){return applicationContext;}public static Object getBean(String beanName) throws BeansException{return applicationContext.getBean(beanName);} }
JSP自定义select标签 字典表数据相关推荐
- JSP自定义select标签中取值
JSP自定义select标签的取值 select取值是通过name属性获取. 自定义标签的流程: 1.继承JSP标签的标签类TagSupport,SimpleTagSupport等,重写dostart ...
- 实现自定义select标签
我们经常会遇到这种场景: 假设项目中有一个分类对象,类似数据字典,一个code对应一个name.这个在页面作为一个过滤条件:选择分类.一般做法是在jsp页面直接写select标签,用opt ...
- 自定义select标签
1:定义tid文件 <tag><name>select</name><tag-class>com.zhoujun.jsp.day02.SelectTag ...
- springboot+thymeleaf自定义select标签
效果 <sys:dict type="DISEASES_OPTION" name="option_code" class="" /&g ...
- redis初始化存储数据库字典表数据设计方案
随着项目访问量的并发,对数据库操作越来越多,为了优化系统,将原数据库字典表提到了JVM内存里,但是随着开始着手集群后,弊端就出来了!最近想着将字典数据放到redis里,并且能够尽量实现多场景便捷读取. ...
- html select 修改默认箭头样式,自定义select标签箭头样式
select::-ms-expand{ display: none; }//ie样式清除 select{ appearance:none; -moz-appearance:none; -webkit- ...
- HTML select标签提交表单
一.参考 https://www.cnblogs.com/liyihua/p/12363804.html 二.验证 https://www.w3cschool.cn/htmltags/tag-sele ...
- select标签动态获取数据时如何控制下拉框长度
<td>队列ID</td><td><select id="select_queue" panelHeight="100" ...
- springboot在项目启动时加载字典表数据进map内存,真实测试通过。
public class DicMap {@Autowiredprivate static CustomerDao customerDao;private static HashMap<Stri ...
- 如何通过字典表来获取下拉数据的实现
①在web.xml中添加监听,启动的时候初始化. 1 2 3 4 <!--Web ApplicationContext 载入,继承处Spring的ApplicationContextListen ...
最新文章
- 微型计算机m3500q,拆分式一体机!联想ThinkCentre 超级Q 23
- logback:用slf4j+logback实现多功能日志解决方案
- mongodb 备份压缩_MongoDB 如何实现备份压缩
- 从零写一个编译器(三):语法分析之几个基础数据结构
- 监听vuex的某条数据
- 华为交换机CPU SNMP OID [简单整理]
- shell mysql awk_shell mysql 处理数据小结
- 伴生对象-apply方法
- JavaEE 笔记一
- Tomcat8.0进入tomcat manager的方法
- java企业员工管理系统_基于JavaWeb的企业员工信息管理系统的设计任务书
- SpringCloud-Feign接口调用超时:feign.RetryableException: Connection timed out (Connection timed out) execut
- 规格说明书练习-吉林市1日游
- 函数的原函数,可积性和变上限积分之间的区别和联系
- button loading indicators
- php 定义宏函数,汇编语言宏函数
- 海思开发板遇到的问题启发性的链接
- pytorch版本CSNet运行octa数据集的问题
- HBuilder X 下载安装内置浏览器失败
- 元宇宙为金融带来哪些想象力?将为银行、保险、证券业打开新版图