jsp自定义select选择标签和数据字典

原文地址:http://www.360doc.com/content/12/1130/12/8189294_251151414.shtml

               

                   http://my.oschina.net/weiweijava/blog/214698

jsp自定义标签,用于项目便捷开发。在实际项目开发中,我们大多数时候会采用数据字典来储存项目中一些数据,比如性别、国际、类型等,用数据字典存储很方便,因此在数据库中添加一张数据字典表t_dict_value,有做过的项目中采用两张表进行数据字典的管理,个人在设计数据字典的时候感觉一张表也够用了,不多说看建表语句:

?
1
2
3
4
5
6
7
8
CREATE  TABLE  `t_dict_value` (
   `id`  varchar (36)  NOT  NULL ,
   `dict_name`  varchar (36)  default  NULL ,
   `item_code`  varchar (36)  default  NULL ,
   `item_desc`  varchar (36)  default  NULL ,
   PRIMARY  KEY   (`id`),
   UNIQUE  KEY  `item_code` (`item_code`)
) ENGINE=MyISAM  DEFAULT  CHARSET=gbk;

我的自定义标签是基于数据字典表使用的,当然后续业务中有需要也可以制作特定的自定义标签,接下来开始自定义标签,首先写一个DictSelectTag类,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package  com.infopatent.juangetljc.web.controller.core;
 
import  java.io.IOException;
import  java.util.List;
 
import  javax.servlet.jsp.JspException;
import  javax.servlet.jsp.JspWriter;
import  javax.servlet.jsp.tagext.TagSupport;
 
import  org.apache.commons.lang3.StringUtils;
 
import  com.infopatent.juangetljc.core.DictValue;
 
public  class  DictSelectTag  extends  TagSupport  {
 
     private  String dictName;
     private  boolean  defaultValue;
     private  String value;
     private  String name;
     private  String id;
     private  String cssClass;
     private  String styleClass;
     private  String multiple;
     private  String onChange;
 
     
     
     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 getMultiple() {
         return  multiple;
     }
 
     public  void  setMultiple(String multiple) {
         this .multiple = multiple;
     }
 
     public  String getOnChange() {
         return  onChange;
     }
 
     public  void  setOnChange(String onChange) {
         this .onChange = onChange;
     }
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
 
     public  String getId() {
         return  id;
     }
 
     public  void  setId(String id) {
         this .id = id;
     }
 
     public  String getValue() {
         return  value;
     }
 
     public  void  setValue(String value) {
         this .value = value;
     }
 
     public  String getDictName() {
         return  dictName;
     }
 
     public  void  setDictName(String dictName) {
         this .dictName = dictName;
     }
     
     public  boolean  isDefaultValue() {
         return  defaultValue;
     }
 
     public  void  setDefaultValue( boolean  defaultValue) {
         this .defaultValue = defaultValue;
     }
     
     
     @Override
     public  int  doEndTag()  throws  JspException{
         DictValue dict =  new  DictValue();
         List<DictValue> dict_list = dict.getRepository().findByProperty(DictValue. class , "dictName" ,dictName);
         JspWriter out = pageContext.getOut();
         StringBuffer sb =  new  StringBuffer();
         sb.append( "<select name='" + this .getName()+ "' id='" + this .getId()+ "'" );
         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( "onchange=\""  this .getOnChange() +  "\" " );
         }
         sb.append( ">" );
         if ( this .isDefaultValue()){  
             sb.append( "<option value=''>--请选择--</option>" );  
         }
         for (DictValue dc:dict_list){
             if (dc.getItemCode().equals( this .getValue())){
                 sb.append( "<option value='" +dc.getItemCode()+ "' selected='selected'>" );
             } else {
                 sb.append( "<option value='" +dc.getItemCode()+ "'>" );
             }
             sb.append(dc.getItemDesc()+ "</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;
     }
     
}

再写一个DictLabelTag类用于显示字典中的值,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package  com.infopatent.juangetljc.web.controller.core;
 
import  java.io.IOException;
 
import  javax.servlet.jsp.JspException;
import  javax.servlet.jsp.JspWriter;
import  javax.servlet.jsp.tagext.TagSupport;
 
import  org.springframework.web.servlet.tags.form.OptionsTag;
 
import  com.infopatent.juangetljc.core.DictValue;
 
public  class  DictLabelTag  extends  TagSupport {
 
     private  String dictName =  "" ;
     private  String itemCode;
 
     public  String getDictName() {
         return  dictName;
     }
 
     public  void  setDictName(String dictName) {
         this .dictName = dictName;
     }
 
     public  String getItemCode() {
         return  itemCode;
     }
 
     public  void  setItemCode(String itemCode) {
         this .itemCode = itemCode;
     }
 
     @Override
     public  int  doEndTag()  throws  JspException {
         DictValue dict =  new  DictValue();
         JspWriter out = pageContext.getOut();
         try  {
             out.write(dict.codeToName(getDictName(),getItemCode()));
         catch  (IOException e) {
             throw  new  JspException(e);
         }
         return  TagSupport.EVAL_PAGE;
     }
}

接下来配置tld文件,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
dict.tld:
<?xml version= "1.0"  encoding= "UTF-8" ?>
<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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
         version= "2.0" >
 
     <description>SPay JSP Form Tag Library</description>
     <tlib-version> 1.0 </tlib-version>
     < short -name>dict</ short -name>
     <uri>http: //www.tljc.com/dict_tag</uri>
 
     <tag>
         <description>Renders an HTML  'select'  element. Supports databinding to the selected option.</description>
         <name>select</name>
         <tag- class >com.infopatent.juangetljc.web.controller.core.DictSelectTag</tag- class >
         <body-content>JSP</body-content>
         <attribute>  
                <name>defaultValue</name>  
             <required> true </required>  
             <rtexprvalue> true </rtexprvalue>  
         </attribute>  
     <attribute>  
             <name>value</name>  
             <required> false </required>  
             <rtexprvalue> true </rtexprvalue>  
     </attribute>
     <attribute>
         <name>dictName</name>
         <required> true </required>  
         <rtexprvalue> true </rtexprvalue>
     </attribute>
     <attribute>
         <name>name</name>
         <required> true </required>  
         <rtexprvalue> true </rtexprvalue>
     </attribute>
     <attribute>
         <name>id</name>
         <required> true </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>
     <attribute>
         <name>multiple</name>
         <required> false </required>  
         <rtexprvalue> true </rtexprvalue>
     </attribute>
     <attribute>
         <name>onChange</name>
         <required> false </required>  
         <rtexprvalue> true </rtexprvalue>
     </attribute>
     </tag>
     
     
 
     <tag>
         <name>itemdesc</name>
         <tag- class >com.infopatent.juangetljc.web.controller.core.DictLabelTag</tag- class >
         <body-content>JSP</body-content>
         <attribute>
             <description>The Dict Name config in dict_value</description>
             <name>dictName</name>
             <required> true </required>
             <rtexprvalue> true </rtexprvalue>
         </attribute>
         <attribute>
             <description>The Dict Code config in dict_value</description>
             <name>itemCode</name>
             <required> true </required>
             <rtexprvalue> true </rtexprvalue>
         </attribute>
     </tag>
</taglib>

接下来在jsp页面里引入标签:

?
1
<% @taglib  prefix= "dict"  uri= "http://www.tljc.com/dict_tag"  %>

这样便可以在jsp页面里使用定义的标签了:

?
1
<dict:select defaultValue= "true"  name= "GJ"  id= "GJ"  dictName= "GJ"  cssClass= "form-control" />

前提是要在字典表里加上“GJ”这条数据。

OK了!



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值