将jasperReport设计文件和编译文件放到webRoot下面的jasper文件夹下面。
public class Person {
private String person_Id;
private String person_name;
private String person_age;
private String person_address;
}
public class PersonService {
public List<Person> getAllPerson() {
List<Person> perList = new ArrayList<Person>();
perList.add(new Person("101", "小博", "22", "湖北"));
perList.add(new Person("102", "张三", "21", "湖南"));
perList.add(new Person("103", "李四", "23", "江苏"));
perList.add(new Person("104", "王五", "22", "上海"));
return perList;
}
}
构造我们的applet如下:
public class JRPrinterApplet extends javax.swing.JApplet {
private URL url = null;
public void init() {
String strUrl = getParameter("REPORT_URL");
if (strUrl != null) {
try {
url = new URL(getCodeBase(), strUrl);// 从获得html参数中获得报表URL
// System.out.println("url=" + url.toURI());//要是servlet的路径
} catch (Exception e) {
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
} else {
JOptionPane.showMessageDialog(this, "Source URL not specified");
}
}
public void start() {
if (url != null) {
try {
Object obj = JRLoader.loadObject(url);// 发送对象请求,获得JasperPrint对象
JasperPrintManager.printReport((JasperPrint) obj, true);// 调用方法打印所获得的JasperPrint对象
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
构造我们的action如下:
public class JRPrintAction extends ActionSupport {
@Override
public String execute() throws Exception {
File reportFile = new File(ServletActionContext.getRequest()
.getRealPath("/jasper/preson.jasper"));
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("year", "2009");
parameters.put("unit_mc", "武汉XX科技有限公司");
List<Person> personList = new PersonService().getAllPerson();
JasperPrint jasperPrint = null;
try{
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(reportFile);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
new JRBeanCollectionDataSource(personList));
}catch (Exception e) {
throw e;
}
if(null != jasperPrint){
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
}
return null;
}
}
struts.xml配置如下:
<action name="jrPrint"
class="com.mengya.action.JRPrintAction">
<result name="success">/index.jsp</result>
</action>
web.xml配置如下:
<filter> <filter-name>Struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>Struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
将JRPrinterApplet的编译文件放到webRoot下面的applet文件夹下面
页面调用如下:
<input type="button" value="打印" name="button1" οnclick="print();">
<script>
function print(){
var url = "jrPrint.action";
document.write('<APPLET ID="JrPrt" CODE = "JRPrinterApplet.class" CODEBASE = "applets" ARCHIVE = "jasperreports-3.5.2-applet.jar,commons-logging-1.0.2.jar,commons-collections-2.1.jar" WIDTH = "0" HEIGHT = "0">');
document.write('<PARAM NAME = "type" VALUE="application/x-java-applet;version=1.2.2">');
document.write('<PARAM NAME = "scriptable" VALUE="false">');
document.write('<PARAM NAME = "REPORT_URL" VALUE ="'+url+'">');
document.write('</APPLET>');
}
</script>