写一个JSP标签,一个Java文件,一个标签定义,避免重复写好多嵌入FusionChartsFree的代码。
第一步:定义标签属性等信息,编写TLD文件;
第二步:编写标签处理的代码;
第三步:测试标签;
第四步:打包发布。
关键: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
|
<?
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
>jrtz</
short-name
>
<
uri
>http://www.sunrise.com/jrtz</
uri
>
<
tag
>
<
name
>fcf</
name
>
<
tag-class
>com.sunrise.broncho.tag.FusionChart</
tag-class
>
<
body-content
>JSP</
body-content
>
<
description
>
<![CDATA[FusionChartsFree 图表组件应用在JSP页面]]>
</
description
>
<
attribute
>
<
name
>chartSWF</
name
>
<
required
>true</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[FusionChart的模版图例文件名]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>divId</
name
>
<
required
>true</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[图表所显示在的Div的Id]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>chartId</
name
>
<
required
>true</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[图表的名称Id]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>dataXml</
name
>
<
required
>false</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[数据源信息,XML数据源.如果使用XML数据源时,URL和XML同时存在优先使用XML数据源]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>dataUrl</
name
>
<
required
>true</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[数据源信息,URL数据源.如果使用XML数据源时该参数设为:""即可]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>chartWidth</
name
>
<
required
>false</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[图标显示的宽,默认值为300]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>chartHeight</
name
>
<
required
>false</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[图标显示的高,默认值为180]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>deCode</
name
>
<
required
>false</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[注意:仅在使用URL数据源下使用,对URL进行编码或解码,ture为解码,false为编码,默认值为false]]>
</
description
>
</
attribute
>
<
attribute
>
<
name
>charName</
name
>
<
required
>false</
required
>
<
rtexprvalue
>true</
rtexprvalue
>
<
description
>
<![CDATA[注意:仅在使用URL数据源下使用,对URL进行编码解码的处理的字符名称,默认为:UTF-8]]>
</
description
>
</
attribute
>
</
tag
>
</
taglib
>
|
FusionChartsFree的相关:http://aiilive.blog.51cto.com/1925756/1267021
关键: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
|
import
java.io.IOException;
import
java.io.UnsupportedEncodingException;
import
java.net.URLDecoder;
import
java.net.URLEncoder;
import
javax.servlet.jsp.JspException;
import
javax.servlet.jsp.tagext.TagSupport;
/**
* 使用FusionChartsFree图标组件的标签支持类
*
* @author ZhangXiao
* @time 2013-8-12
*/
public
class
FusionChart
extends
TagSupport {
/**
*
*/
private
static
final
long
serialVersionUID = -455570295257618661L;
private
String chartSWF =
""
;
private
String divId =
""
;
private
String dataUrl =
null
;
private
String dataXml =
null
;
private
String chartId = divId +
"chart"
;
private
int
chartWidth =
300
;
private
int
chartHeight =
180
;
private
boolean
deCode =
false
;
private
String charName =
"UTF-8"
;
@Override
public
int
doStartTag()
throws
JspException {
try
{
byte
[] script = createScript().getBytes();
pageContext.getOut().write(
new
String(script,
"UTF-8"
));
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
super
.doStartTag();
}
/**
* 通过标签参数创建JavaScript脚本信息
*
* @return 返回图表渲染脚本
*/
private
String createScript() {
StringBuffer sb =
new
StringBuffer();
sb.append(
"<script type='text/javascript'>"
);
sb.append(
"var fcf=new FusionCharts("
);
sb.append(
"'"
);
sb.append(chartSWF);
sb.append(
"', "
);
sb.append(
"'"
);
sb.append(chartId);
sb.append(
"', "
);
sb.append(
"'"
);
sb.append(chartWidth);
sb.append(
"', "
);
sb.append(
"'"
);
sb.append(chartHeight);
sb.append(
"' ); "
);
if
((
this
.dataUrl ==
null
&&
this
.dataXml ==
null
)
|| (
this
.dataUrl ==
""
&&
this
.dataXml ==
""
)) {
sb =
new
StringBuffer();
sb.append(
"无有效数据支持!"
);
}
else
{
// 数据源的选取,XML和URL都存在时:优先选择XML
if
(
this
.dataXml !=
null
) {
sb.append(
"fcf.setDataXML(\""
);
sb.append(
this
.dataXml);
sb.append(
"\"); "
);
}
else
{
sb.append(
"fcf.setDataURL('"
);
if
(!
this
.deCode) {
sb.append(
this
.encode(
this
.dataUrl));
}
else
{
sb.append(
this
.decode(
this
.dataUrl));
}
sb.append(
"'); "
);
}
sb.append(
"fcf.render('"
);
sb.append(
this
.divId);
sb.append(
"'); "
);
sb.append(
"</script>"
);
}
return
sb.toString();
}
/**
* 对URL进行解码
*
* @param url
* @return 返回解码字符串
*/
private
String decode(String url) {
try
{
return
URLDecoder.decode(url,
this
.charName);
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
return
url;
}
/**
* 对URL进行编码
*
* @param url
* @return 返回编码字符串
*/
private
String encode(String url) {
try
{
return
URLEncoder.encode(url,
this
.charName);
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
return
url;
}
}
|
关于测试参见附件例子FusionChartsFree JSP Tag web工程,例子文件要去掉.txt后缀。
本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1285886,如需转载请自行联系原作者