In this example, you will see the how to create PDF in struts, and how to write message in generated PDF file in struts2.
For this, we requires a jar file iText.jar. It is used in java for generating PDF.
index.jspADS_TO_REPLACE_1
< html>< head>< meta http-equiv="Refresh" content="0;URL=message.action">ADS_TO_REPLACE_2< title>Generate_PDF_in_Struts</title></ head>< body><h1>Generate_PDF_in_Struts</h1><hr>ADS_TO_REPLACE_3< h2 style="color: green;">Loading.....</h2></body> </html> |
MessageJSp.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"ADS_TO_REPLACE_4 pageEncoding="ISO-8859-1"%><%@ taglib uri="/struts-tags" prefix="s"%><! DOCTYPE html PUBLIC ADS_TO_REPLACE_5"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head><title>Generate_PDF_in_Struts</title></head> <body><h1>Generate_PDF_in_Struts</h1><hr>ADS_TO_REPLACE_6
< </ body></ html> |
GeneratePDFAction.java (Action Class)
package net.roseindia.action;import java.io.*;ADS_TO_REPLACE_8import net.roseindia.model.MessageInPDF;import com.lowagie.text.Document;import com.lowagie.text.Paragraph;ADS_TO_REPLACE_9import com.lowagie.text.pdf.PdfWriter;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;ADS_TO_REPLACE_10public class GeneratePDFAction extends ActionSupport implements ModelDriven {MessageInPDF obMessage; public String execute() throws Exception {ADS_TO_REPLACE_11String string = obMessage.getMessage();Document document = new Document();PdfWriter.getInstance(document, new FileOutputStream(ADS_TO_REPLACE_12 "c:\\StrutsExample.pdf"));document.open(); document.add( new Paragraph(string));ADS_TO_REPLACE_13document.close(); return SUCCESS; }@OverrideADS_TO_REPLACE_14 public Object getModel() { obMessage = new MessageInPDF(); // TODO Auto-generated method stubADS_TO_REPLACE_15 return obMessage; } } |
MessageInPDF.java (Model)
package net.roseindia.model;public class MessageInPDF {ADS_TO_REPLACE_16 private String message; public String getMessage() { return message; }ADS_TO_REPLACE_17 public void setMessage(String message) { this.message = message; } } |
struts.xmlADS_TO_REPLACE_18
< struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" />ADS_TO_REPLACE_19<package name="default" namespace="/" extends="struts-default"> <action name="message">ADS_TO_REPLACE_20 <result>/jsp/MessageJSp.jsp</result> </action> <action name="generatepdf" class="net.roseindia.action.GeneratePDFAction">ADS_TO_REPLACE_21 <result name="input">/jsp/MessageJSp.jsp</result> <result>jsp/success.jsp</result> </action>ADS_TO_REPLACE_22 </package>
</ struts> |
RegistrationSuccess.jspADS_TO_REPLACE_23
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><! DOCTYPE html PUBLIC ADS_TO_REPLACE_24"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head> <meta
http-equiv="Content-Type"
content="text/html;
charset=ISO-8859-1">ADS_TO_REPLACE_25
< <body><h1>Generate_PDF_in_Struts</h1><hr> <h3
style="color:
green;">PDF
successfully Generated......</h3>ADS_TO_REPLACE_26
</ </html |
Output
ADS_TO_REPLACE_27
ADS_TO_REPLACE_28
Advertisements
Ads
Ads