1、jar包
spring-webmvc-3.0.5.RELEASE.jar
jsp-api-2.1.jar
下载地址:http://download.csdn.net/detail/yjqyyjw/9569039
maven引用
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
2、java类的写法
package com.gfan.account.check.tag;
import java.io.IOException;
import java.util.List;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
import com.gfan.account.check.model.RepDict;
import com.gfan.account.check.service.DictTagService;
public class DictTag extends RequestContextAwareTag{
/**
* @Fields serialVersionUID
*/
private static final long serialVersionUID = 4287584103783211416L;
private String dictType;
private String id;
private String style;
private String name;
private String tagClass;
private String value;
private String nullStr;
public void setDictType(String dictType) {
this.dictType = dictType;
}
public void setId(String id) {
this.id = id;
}
public void setStyle(String style) {
this.style = style;
}
public void setNullStr(String nullStr) {
this.nullStr = nullStr;
}
public void setName(String name) {
this.name = name;
}
public void setTagClass(String tagClass) {
this.tagClass = tagClass;
}
public void setValue(String value) {
this.value = value;
}
@Override
protected int doStartTagInternal() throws Exception {
DictTagService dictTagService = (DictTagService)this.getRequestContext().getWebApplicationContext().getBean("dictTagService");
List<RepDict> entrys = dictTagService.getDicts(dictType);
try {
StringBuilder sb = new StringBuilder("<select id='"+id+"' name='"+name+"' ");
if(style!=null&&!style.equals(""))
sb.append(" style='"+style+"'>");
if(value!=null&&!value.equals(""))
sb.append(" value='"+value+"'>");
if(tagClass!=null&&!tagClass.equals(""))
sb.append(" class='"+tagClass+"'>");
sb.append(" >");
if(nullStr!=null&&!nullStr.equals("")){
sb.append("<option value=''>"+nullStr+"</option>");
}
for(RepDict entry:entrys){
sb.append("<option value='"+entry.getDictKey()+"'>"+entry.getDictValue()+"</option>");
}
sb.append("</select>");
pageContext.getOut().write(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
}
3、tld配置文件的写法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>yjq</short-name>
<!-- 引用路径 例:<%@ taglib uri="http://www.dictTag.com" prefix="yjq" %> -->
<uri>http://www.dictTag.com</uri>
<description>this is yjq's tld lib</description>
<tag>
<!-- 这个tag的名字,关系到jsp页面的引用 <yy:select> -->
<name>select</name>
<!-- 这个Tag是由那个类实现的,这个class可以在struts.jar包中找到 -->
<tag-class>com.gfan.account.check.tag.DictTag</tag-class>
<!--body-content有三个可选值
1."jsp" 表示标签体由其他jsp元素组成
如果其有jsp元素,那么标签会先解释,然后将元素的实际值传入。
比如标签体里含有<%=attributeName%>这样子的jsp元素,
此时标签会按attributeName的实际值是什么就传入什么。这个是最常用的一个。
2."empty" 标签体必须为空
在引用这个Tag的时候,可以<yy:select id="attributeName" />,
而不必<yy:select id="attributeName" ></yy:select>
3."tagdependent" 由标签解释,不带jsp转换 -->
<body-content>empty</body-content>
<!-- <attribute> </attribute>这里标识的是这个Tag的一个参数。
例如<yy:select id="attributeName" />中的id -->
<attribute>
<!-- 这个参数的名字 -->
<name>dictType</name>
<!-- 这个参数是否是必填项, 如果为true则必须写这个参数,否则会报错 -->
<required>true</required>
<!-- 是说这个标签的值是否可以动态赋值,如value="<%=attributeName%>" -->
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>style</name>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>tagClass</name>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>nullStr</name>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
注:1.配置文件中属性值与实现类中的属性值,jsp中使用的属性值名称都是一一对应的
例如 id ,name 配置文件中attribute配置上,实现类要实现set方法,jsp中才可以应用<yy:select id="projectMode"/>
2.放入WEB-INF文件夹下不需要任何配置引用
3、jsp中的应用
头引用(引用头信息之后,输入“<yjq:”就会出现提示)
<%@ taglib uri="http://www.dictTag.com" prefix="yjq" %>
body使用
<yy:select dictType="projectMode" id="projectMode" name="projectMode" tagClass="m-wrap medium"/>
4、数据库中字典表的设计
/*
Navicat MySQL Data Transfer
Source Server : 本地
Source Server Version : 50532
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50532
File Encoding : 65001
Date: 2016-07-25 11:30:19
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for rep_dict
-- ----------------------------
DROP TABLE IF EXISTS `rep_dict`;
CREATE TABLE `rep_dict` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`DICT_TYPE` varchar(32) DEFAULT NULL COMMENT '字典名称',
`DICT_KEY` varchar(64) DEFAULT NULL COMMENT '实际值',
`DICT_ORDER` int(9) DEFAULT NULL COMMENT '排序',
`DICT_VALUE` varchar(64) DEFAULT NULL COMMENT '显示值',
`INSERT_TIME` datetime DEFAULT NULL COMMENT '时间',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=93 DEFAULT CHARSET=utf8 COMMENT='业务字典';
-- ----------------------------
-- Records of rep_dict
-- ----------------------------
INSERT INTO `rep_dict` VALUES ('50', 'productType', '影音播放', null, '影音播放', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('51', 'productType', '金融理财', null, '金融理财', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('52', 'productType', '电子办公', null, '电子办公', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('59', 'projectMode', 'CPC', null, 'CPC', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('60', 'projectMode', 'CPM', null, 'CPM', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('61', 'projectMode', '其他', null, '其他', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('62', 'channelType', '广告公司', null, '广告公司', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('63', 'channelType', '直营渠道', null, '直营渠道', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('64', 'channelType', '网络联盟', null, '网络联盟', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('73', 'mediaType', '应用商店', null, '应用商店', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('74', 'mediaType', 'WAP站', null, 'WAP站', '2016-07-06 15:40:20');
INSERT INTO `rep_dict` VALUES ('75', 'mediaType', 'WEB站', null, 'WEB站', '2016-07-06 15:40:20');