[Struts2]JasperReports Plugin设定PDF加密

言:
Struts2怎麽整合JasperReports Plugin就不多说了, 有兴趣请参考这篇
http://www.javaworld.com.tw/jute/post/view?bid=42&id=186689&sty=2

如果要设定PDF加密肯定就要修改JasperReportsResult,
不过细心一点你就会发现Struts2.1.2之後就有支援PDF加密 , 只是API Document写的很不清楚, 请参网址
http://struts.apache.org/2.1.2/struts2-plugins/struts2-jasperreports-plugin/apidocs/org/apache/struts2/views/jasperreports/JasperReportsResult.html

需求:
1. Struts2 2.0.x+
2. Struts2 2.1.2+ Source Code
3. jasperreports

使用教学:
1. 下载Struts 2.1.2之後的Source Code
http://apache.cdpa.nsysu.edu.tw/struts/source/struts-2.1.6-src.zip
http://struts.apache.org/download.cgi

2. 复制org.apache.struts2.views.jasperreports.JasperReportsResult.java到你的专案里面

3. 把JasperReportsResult.java里面Log的部份拿掉(因为我的专案没有用到这个Logger Library所以会出错)

4. 设定struts.xml
?
1
<font color= "#555555" >1<br>2<br>3<br>4<br></font>
?
1
<result name=<font class= "java-string" > "success" </font> type=<font class= "java-string" > "jasper" </font>>      <param name=<font class= "java-string" > "exportParameters" </font>>exportMap</param>    ...</result>


5. 修改Action, 新增以下method
?
1
<font color= "#555555" >1<br>2<br>3<br>4<br>5<br>6<br></font>
?
1
<font class= "java-reserved_word" >public</font> Map<JRPdfExporterParameter, Object> getExportMap() <font class= "java-bracket" >{</font>??Map<JRPdfExporterParameter, Object> exportMap = <font class= "java-reserved_word" > new </font> HashMap<JRPdfExporterParameter, Object>();??exportMap.put(JRPdfExporterParameter.IS_ENCRYPTED, <font class= "java-reserved_word" > true </font>);??exportMap.put(JRPdfExporterParameter.USER_PASSWORD, <font class= "java-string" > "520" </font>);??<font class= "java-reserved_word" > return </font> exportMap;<font class= "java-bracket" >}</font>


6. 也可以对他作其他PDF的设定
?
1
<font color= "#555555" >1<br>2<br>3<br></font>
?
1
exportMap.put(JRPdfExporterParameter.IS_128_BIT_KEY, <font class= "java-reserved_word" > true </font>);exportMap.put(JRPdfExporterParameter.OWNER_PASSWORD, <font class= "java-string" > "5201314" </font>);exportMap.put(JRPdfExporterParameter.PERMISSIONS, PdfWriter.AllowDegradedPrinting);


心得:
幸好Apache Struts2更新的快, 不然又要自己改Source code了...
另外, 如果可以把改过的JasperReportsResult.java Source Code放上来, 请通知我一下, 我再把code放上来

参考:
http://struts.apache.org/2.1.2/struts2-plugins/struts2-jasperreports-plugin/apidocs/org/apache/struts2/views/jasperreports/JasperReportsResult.html

你可能感兴趣的:(加密,object,struts,Class,action,library)