?????????? 轉載自
http://www.blogjava.net/bulktree/archive/2008/12/17/246786.html
???????????? 提起scriptlet就不能不聯想到它的強大功能,jasperReport也是支持scriptlet的哦,先分析一下JasperReport的API吧!
在填充報表時scriplet是一個非常有力的工具,JRAbstractScriptlet.java位于net.sf.jasperreports.engine包下是一個抽象
類
beforeReportInit()?,afterReportInit()?,beforePageInit(),afterPageInit(),?beforeColumnInit(),?afterColumnInit()?,beforeGroupInit(String?groupName),afterGroupInit(String?groupName)?
看看這些名字就知道你能完成那些功能,這幾個方法是要求我們實現的,jasperReport給我們提供了一個實現類JRDefaultScriptlet.java,默認的空實現了上面幾個方法,它只是很便利的為我們提供了所需的八個方法的空實現。我們寫自己的scriptlet時需要繼承JRDefaultScriptlet.java這個類實現自己的相應的功能即可。
?先來看一個簡單的例子:
先看看模板文件的處理:
新建時填寫的這個類是下面我們要介紹的繼承自JRDefaultScriptlet.java類,也就是在模板文件中我們要加上如下代碼
scriptletClass="org.bulktree.ireport.scriptlet.ScriptletReportDemo"
完整的模板文件如下:scriptletDemo.jrxml
<?
xml?version="1.0"?encoding="UTF-8"??
?>
<!--
?Created?with?iReport?-?A?designer?for?JasperReports?
-->
<!
DOCTYPE?jasperReport?PUBLIC?"http://JasperReports//DTD?Report?Design//EN"?"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"
>
<
jasperReport
?????????
name
="scriptletDemo"
?????????columnCount
="1"
?????????printOrder
="Vertical"
?????????orientation
="Portrait"
?????????pageWidth
="595"
?????????pageHeight
="842"
?????????columnWidth
="535"
?????????columnSpacing
="0"
?????????leftMargin
="30"
?????????rightMargin
="30"
?????????topMargin
="20"
?????????bottomMargin
="20"
?????????whenNoDataType
="NoPages"
?????????scriptletClass
="org.bulktree.ireport.scriptlet.ScriptletReportDemo"
?????????isTitleNewPage
="false"
?????????isSummaryNewPage
="false"
>
????
<
property?
name
="ireport.scriptlethandling"
?value
="2"
?
/>
????
<
property?
name
="ireport.encoding"
?value
="UTF-8"
?
/>
????
<
import?
value
="java.util.*"
?
/>
????
<
import?
value
="net.sf.jasperreports.engine.*"
?
/>
????
<
import?
value
="net.sf.jasperreports.engine.data.*"
?
/>
????
<
parameter?
name
="ReportTitle"
?isForPrompting
="true"
?class
="java.lang.String"
/>
????????
<
background
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
background
>
????????
<
title
>
????????????
<
band?
height
="20"
??isSplitAllowed
="true"
?
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
mode
="Opaque"
????????????????????????x
="193"
????????????????????????y
="0"
????????????????????????width
="134"
????????????????????????height
="18"
????????????????????????backcolor
="#FFCC33"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
textAlignment
="Center"
?verticalAlignment
="Middle"
>
????????????????????????
<
font?
pdfFontName
="Helvetica-Bold"
?size
="12"
?isBold
="true"
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$P{ReportTitle}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????
</
band
>
????????
</
title
>
????????
<
pageHeader
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
pageHeader
>
????????
<
columnHeader
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
columnHeader
>
????????
<
detail
>
????????????
<
band?
height
="264"
??isSplitAllowed
="true"
?
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="85"
????????????????????????y
="20"
????????????????????????width
="329"
????????????????????????height
="61"
????????????????????????forecolor
="#FF0099"
????????????????????????key
="textField-1"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
textAlignment
="Center"
?verticalAlignment
="Middle"
>
????????????????????????
<
font?
pdfFontName
="Helvetica-Bold"
?size
="20"
?isBold
="true"
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$P{REPORT_SCRIPTLET}.showInfor()
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????
</
band
>
????????
</
detail
>
????????
<
columnFooter
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
columnFooter
>
????????
<
pageFooter
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
pageFooter
>
????????
<
summary
>
????????????
<
band?
height
="50"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
summary
>
</
jasperReport
>
下來看看怎么實現我們自己的scriplet
ScriptletReportDemo.java
package
?org.bulktree.ireport.scriptlet;
import
?net.sf.jasperreports.engine.JRDefaultScriptlet;
import
?net.sf.jasperreports.engine.JRScriptletException;
/**
?*?
?*?
@author
?bulktree?Email:?laoshulin@gmail.com
?*?@Nov?26,?2008
?
*/
public
?
class
?ScriptletReportDemo?
extends
?JRDefaultScriptlet?
{
????@Override
????
public
?
void
?afterColumnInit()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************afterColumnInit()**************************************
"
);
????}
????@Override
????
public
?
void
?afterDetailEval()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************afterDetailEval()**************************************
"
);
????}
????@Override
????
public
?
void
?afterGroupInit(String?groupName)?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************afterDetailEval()**************************************
"
);
????}
????@Override
????
public
?
void
?afterPageInit()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************afterPageInit()**************************************
"
);
????}
????@Override
????
public
?
void
?afterReportInit()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************afterReportInit()?begin**************************************
"
);
????????
????????String?str?
=
?(String)?
this
.getParameterValue(
"
ReportTitle
"
);
????????System.out.println(
"
report?title=====>>>>
"
+
str);
????????
????????str?
+=
?str.subSequence(
0
,?str.length()
-
2
);
????????
this
.setParameterValue(
"
ReportTitle
"
,?str);
????????System.out.println(
"
**************************************afterReportInit()?end**************************************
"
);
????}
????@Override
????
public
?
void
?beforeColumnInit()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************beforeColumnInit()**************************************
"
);
????}
????@Override
????
public
?
void
?beforeDetailEval()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************beforeDetailEval()**************************************
"
);
????}
????@Override
????
public
?
void
?beforeGroupInit(String?groupName)?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************beforeGroupInit()**************************************
"
);
????}
????@Override
????
public
?
void
?beforePageInit()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************beforePageInit()**************************************
"
);
????}
????@Override
????
public
?
void
?beforeReportInit()?
throws
?JRScriptletException?
{
????????System.out.println(
"
**************************************beforeReportInit()**************************************
"
);
????}
????
public
?String?showInfor()?
throws
?JRScriptletException?
{
????????
return
?
"
the?is?scriptlet?scriptlet?scriptlet?the,sscriptlet?report?the?is?ascriptlet?report?this?is?a?scriptlet?report?this?is?a?scriptlet?report
"
;
????}
????
}
這段代碼最后一個方法是我們自己的加的,用來在報表上顯示一段文本。我們知道對于一個Field、Parameter、Variable,JasperReport分別采用$F{FieldName}、$P{Parametername}、$V{VariableName}來引用,而如果要引用ScriptletReportDemo.java類的showInfor()返回字符串顯示在報表上,看看這個就知道了
在afterReportInit方法中我們把parameter字段取出來前后添上五個*號再set進去
????下來寫一個test類測試一下:
package
?org.bulktree.ireport.scriptlet;
import
?java.io.File;
import
?java.io.FileInputStream;
import
?java.io.InputStream;
import
?java.util.HashMap;
import
?net.sf.jasperreports.engine.JREmptyDataSource;
import
?net.sf.jasperreports.engine.JasperCompileManager;
import
?net.sf.jasperreports.engine.JasperFillManager;
import
?net.sf.jasperreports.engine.JasperPrint;
import
?net.sf.jasperreports.engine.JasperReport;
import
?net.sf.jasperreports.view.JasperViewer;
/**
?*?
?*?
@author
?bulktree?Email:?laoshulin@gmail.com
?*?@Nov?27,?2008
?
*/
public
?
class
?ScriptletTestMain?
{
????
public
?
static
?
void
?main(String[]?args)?
{
????????String?path?
=
?
"
D:/workspace/scriptletDemo.jrxml
"
;
????????File?file?
=
?
new
?File(path);
????????InputStream?in;
????????
try
?
{
????????????HashMap?parameters?
=
?
new
?HashMap();
????????????parameters.put(
"
ReportTitle
"
,?
"
LAOSHULIN
"
);
????????????in?
=
?
new
?FileInputStream(file);
????????????JasperReport?jasperReport?
=
?JasperCompileManager.compileReport(in);
????????????JasperPrint?jasperPrint?
=
?JasperFillManager.fillReport(jasperReport,
????????????????????parameters,?
new
?JREmptyDataSource());
????????????JasperViewer?viewer?
=
?
new
?JasperViewer(jasperPrint);
????????????viewer.setVisible(
true
);
????????}
?
catch
?(Exception?e)?
{
????????????
//
?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}
????}
}
效果不錯吧:
getParameterValue、setParameterValue方法可以操作Parameter,Field/Variable該怎么set呢?
????看看?JRAbstractScriptlet.java類的這個方法:
public
?
void
?setData(
????????Map?parsm,
????????Map?fldsm,
????????Map?varsm,
????????JRFillGroup[]?grps
????????)
????
{
????????parametersMap?
=
?parsm;
????????fieldsMap?
=
?fldsm;
????????variablesMap?
=
?varsm;
????????groups?
=
?grps;
????}
????????似乎有點意思,我們可以通過這個方法把我們期望的數據組裝成Map然后set進去,可是要只針對個別字段怎么處理呢,調用此方法似乎不太合常理,仔細查看API卻沒有實際能調用的API吧!這個似乎不太合乎,仔細看看確實沒有調用的,至少目前我還是沒有發現,怎么辦 自己寫吧!
????設置Field方法:
public
?
void
?setFieldValue(String?fieldName,?Object?value)?
throws
?JRScriptletException
????
{
????????JRFillField?field?
=
?(JRFillField)
this
.fieldsMap.get(fieldName);
????????
if
?(field?
==
?
null
)
????????
{
????????????
throw
?
new
?JRScriptletException(
"
FieldName?not?found?:?
"
?
+
?fieldName);
????????}
????????
????????field.setValue(value);
????}
????設置Variable方法:
public
?
void
?setVariableValue(String?variableName,?Object?value)?
throws
?JRScriptletException
????
{
????????JRFillVariable?variable?
=
?(JRFillVariable)
this
.variablesMap.get(variableName);
????????
if
?(variable?
==
?
null
)
????????
{
????????????
throw
?
new
?JRScriptletException(
"
Variable?not?found?:?
"
?
+
?variableName);
????????}
????????
????????
if
?(value?
!=
?
null
?
&&
?
!
variable.getValueClass().isInstance(value)?)
????????
{
????????????
throw
?
new
?JRScriptletException(
"
Incompatible?value?assigned?to?variable?
"
?
+
?variableName?
+
?
"
.?Expected?
"
?
+
?variable.getValueClassName()?
+
?
"
.
"
);
????????}
????????
????????variable.setValue(value);
????}
????OK!這樣我們就可以針對報表上的每一個字段處理了,測試通過 代碼就不貼了哦,寫上篇的時候忘記這兩個方法是我自己加的,查看API時才發現所以來了個續