JSP自定义标签使用详解

JSP自定义标签使用详解

简单写一个可以从数据库中读取配置的单选按钮组件

1. Java部分

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
143
144
145
146
package com.pibgstar.demo.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.ArrayList;

/**
* @author pibigstar
* @create 2018-12-02 17:16
* @desc 自定义radio组件
**/
public class RadioTagSupport extends SimpleTagSupport{

private String code;
private String tip;
private String key;
private String value;
private ArrayList<RadioVo> list;

@Override
public void doTag() throws JspException, IOException {
getData();
StringBuilder sp = new StringBuilder();
sp.append("<div class='radio'>");
sp.append("<span class='tip'>");
sp.append(this.getTip());
sp.append("</span>");
for (RadioVo r: list) {
sp.append("<input type='radio' ");
sp.append("name='");
sp.append(r.getName());
sp.append("' value='");
sp.append(r.getValue());
if (r.getChecked()) {
sp.append("' checked />");
}else {
sp.append("' />");
}

sp.append(r.getShowValue());
}
sp.append("</div>");

JspWriter out = getJspContext().getOut();
out.write(sp.toString());
// 从body中获取值
//StringWriter sw = new StringWriter();
//getJspBody().invoke(sw);
//getJspContext().getOut().write(sw.toString());

}

private void getData() {
list = new ArrayList();
// 这里可以通过code值,拿到数据库中自己配置的值,这里只是简单的封装几个固定的值
RadioVo r1 = new RadioVo();
r1.setName("sex");
r1.setValue("1");
r1.setShowValue("男");
RadioVo r2= new RadioVo();
r2.setName("sex");
r2.setValue("2");
r2.setShowValue("女");
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getTip() {
return tip;
}

public void setTip(String tip) {
this.tip = tip;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public ArrayList<RadioVo> getList() {
return list;
}

public void setList(ArrayList<RadioVo> list) {
this.list = list;
}
}


class RadioVo {
private String name;
private String value;
private String showValue;
private Boolean checked;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getShowValue() {
return showValue;
}

public void setShowValue(String showValue) {
this.showValue = showValue;
}

public Boolean getChecked() {
return checked;
}

public void setChecked(Boolean checked) {
this.checked = checked;
}
}

2. 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
<?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>JSP 自定义标签</description>
<display-name>pibigstar core</display-name>
<tlib-version>1.0</tlib-version>
<short-name>pi</short-name>

<tag>
<description>myself radio</description>
<name>radio</name>
<tag-class>com.pibgstar.demo.tags.RadioTagSupport</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>code</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>tip</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>

</taglib>

3. JSP中使用

1
2
3
4
5
6
7
8
9
10
<%@taglib prefix="pi" uri="WEB-INFO/tlds/pibigstar-pi.tld" %>

<html>
<head>
<title>自定义radio标签使用</title>
</head>
<body>
<pi:radio code="SEX" tip="性别"></pi:radio>
</body>
</html>
-------------本文结束感谢您的阅读-------------