无法从会话变量传递值以使用 iText 和 Struts2 制作 pdf
unable to pass values from session variable for making pdf using iText and Struts2
我要做的是制作一些列表,在其中获取值并将其存储在某些特定列表中,因为我想用另一种方法访问这些值,所以我正在创建会话变量并传递通过设置键并使用 get(key) 在另一种方法中访问会话变量中的值,我成功地做到了 so.But 现在我想要的是我想使用列表中的值在另一种方法中制作 pdf。但我做不到 so.Below 是我的代码,请帮我传递制作 pdf 的值。
package com.ca.actions;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import com.ca.database.Database;
import com.ca.pojo.Event;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class DataForGeneralReportsAction extends ActionSupport implements
Preparable, SessionAware {
private List<String> eventsGeneral = new ArrayList<String>();
private List<String> companiesGeneral = new ArrayList<String>();
private SessionMap<String, Object> sessionMapGeneral;
List<String> eventIdList = new ArrayList<String>();
List<String> eventNameList = new ArrayList<String>();
List<String> eventVenueList = new ArrayList<String>();
List<String> eventTimeList = new ArrayList<String>();
List<String> companyNameList = new ArrayList<String>();
List<String> totalAmountList = new ArrayList<String>();
List<String> receivedAmountList = new ArrayList<String>();
List<String> balanceAmountList = new ArrayList<String>();
List<String> eventTdsList = new ArrayList<String>();
List<String> paymentDateList = new ArrayList<String>();
List<String> chequeDdList = new ArrayList<String>();
private String eventGeneral = null;
private String companyGeneral = null;
List<Event> dataForGeneralReports;
public List<String> getEventIdList() {
return eventIdList;
}
public void setEventIdList(List<String> eventIdList) {
this.eventIdList = eventIdList;
}
public List<String> getEventNameList() {
return eventNameList;
}
public void setEventNameList(List<String> eventNameList) {
this.eventNameList = eventNameList;
}
public List<String> getEventVenueList() {
return eventVenueList;
}
public void setEventVenueList(List<String> eventVenueList) {
this.eventVenueList = eventVenueList;
}
public List<String> getEventTimeList() {
return eventTimeList;
}
public void setEventTimeList(List<String> eventTimeList) {
this.eventTimeList = eventTimeList;
}
public List<String> getCompanyNameList() {
return companyNameList;
}
public void setCompanyNameList(List<String> companyNameList) {
this.companyNameList = companyNameList;
}
public List<String> getTotalAmountList() {
return totalAmountList;
}
public void setTotalAmountList(List<String> totalAmountList) {
this.totalAmountList = totalAmountList;
}
public List<String> getReceivedAmountList() {
return receivedAmountList;
}
public void setReceivedAmountList(List<String> receivedAmountList) {
this.receivedAmountList = receivedAmountList;
}
public List<String> getBalanceAmountList() {
return balanceAmountList;
}
public void setBalanceAmountList(List<String> balanceAmountList) {
this.balanceAmountList = balanceAmountList;
}
public List<String> getEventTdsList() {
return eventTdsList;
}
public void setEventTdsList(List<String> eventTdsList) {
this.eventTdsList = eventTdsList;
}
public List<String> getPaymentDateList() {
return paymentDateList;
}
public void setPaymentDateList(List<String> paymentDateList) {
this.paymentDateList = paymentDateList;
}
public List<String> getChequeDdList() {
return chequeDdList;
}
public void setChequeDdList(List<String> chequeDdList) {
this.chequeDdList = chequeDdList;
}
public SessionMap<String, Object> getSessionMapGeneral() {
return sessionMapGeneral;
}
public void setSessionMapGeneral(
SessionMap<String, Object> sessionMapGeneral) {
this.sessionMapGeneral = sessionMapGeneral;
}
public String getEventGeneral() {
return eventGeneral;
}
public void setEventGeneral(String eventGeneral) {
this.eventGeneral = eventGeneral;
}
public String getCompanyGeneral() {
return companyGeneral;
}
public void setCompanyGeneral(String companyGeneral) {
this.companyGeneral = companyGeneral;
}
public List<Event> getDataForGeneralReports() {
return dataForGeneralReports;
}
public void setDataForGeneralReports(List<Event> dataForGeneralReports) {
this.dataForGeneralReports = dataForGeneralReports;
}
public List<String> getEventsGeneral() {
return eventsGeneral;
}
public void setEventsGeneral(List<String> eventsGeneral) {
this.eventsGeneral = eventsGeneral;
}
public List<String> getCompaniesGeneral() {
return companiesGeneral;
}
public void setCompaniesGeneral(List<String> companiesGeneral) {
this.companiesGeneral = companiesGeneral;
}
public DataForGeneralReportsAction() {
// TODO Auto-generated constructor stub
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
Connection con = null;
try {
con = new Database().Get_Connection();
// load companies
PreparedStatement ps = con
.prepareStatement("SELECT DISTINCT company_name FROM event");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
companiesGeneral.add(rs.getString("company_name"));
}
// load events
ps = con.prepareStatement("SELECT DISTINCT event_name FROM event");
rs = ps.executeQuery();
while (rs.next()) {
eventsGeneral.add(rs.getString("event_name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
}
@Override
public String execute() throws Exception {
Connection con = null;
try {
con = new Database().Get_Connection();
// load the table. The first time the table is loaded completely
String sql = "SELECT EVENT_ID, EVENT_NAME, COMPANY_NAME,EVENT_VENUE,TOTAL_AMOUNT,RECEIVED_AMOUNT,EVENT_TDS,BALANCE_AMOUNT,CHEQUE_DD_NO,"
+ "date_format(PAYMENT_DATE,'%d/%m/%Y') as dateAsPayment,EVENT_TIME "
+ "FROM event";
String where = "";
// if instead this action has been called from the JSP page,
// the result is filtered on event and company:
if (eventGeneral != null && companyGeneral != null) {
where = " WHERE event_name = ? AND company_name = ?";
}
// load companies
PreparedStatement ps = con.prepareStatement(sql + where);
if (where.length() > 0) {
ps.setString(1, eventGeneral);
ps.setString(2, companyGeneral);
}
dataForGeneralReports = new ArrayList<Event>();
ResultSet rs = ps.executeQuery();
int i, j = 0;
while (rs.next()) {
dataForGeneralReports.add(new Event(rs.getString("EVENT_ID"),
rs.getString("EVENT_NAME"), rs
.getString("COMPANY_NAME"), rs
.getString("EVENT_VENUE"), rs
.getString("EVENT_TIME"), rs
.getString("TOTAL_AMOUNT"), rs
.getString("RECEIVED_AMOUNT"), rs
.getString("CHEQUE_DD_NO"), rs
.getString("dateAsPayment"), rs
.getString("BALANCE_AMOUNT"), rs
.getString("EVENT_TDS")));
eventIdList.add(rs.getString("EVENT_ID"));
eventNameList.add(rs.getString("EVENT_NAME"));
companyNameList.add(rs.getString("COMPANY_NAME"));
eventVenueList.add(rs.getString("EVENT_VENUE"));
eventTimeList.add(rs.getString("EVENT_TIME"));
totalAmountList.add(rs.getString("TOTAL_AMOUNT"));
receivedAmountList.add(rs.getString("RECEIVED_AMOUNT"));
chequeDdList.add(rs.getString("CHEQUE_DD_NO"));
paymentDateList.add(rs.getString("dateAsPayment"));
eventTdsList.add(rs.getString("EVENT_TDS"));
balanceAmountList.add(rs.getString("BALANCE_AMOUNT"));
}
sessionMapGeneral.put("eventIdPdf", eventIdList);
sessionMapGeneral.put("eventNamePdf", eventNameList);
sessionMapGeneral.put("companyNamePdf", companyNameList);
sessionMapGeneral.put("eventVenuePdf", eventVenueList);
sessionMapGeneral.put("eventTimePdf", eventTimeList);
sessionMapGeneral.put("totalAmountPdf", totalAmountList);
sessionMapGeneral.put("receivedAmountPdf",receivedAmountList);
sessionMapGeneral.put("chequeDdPdf", chequeDdList);
sessionMapGeneral.put("paymentDatePdf",paymentDateList);
sessionMapGeneral.put("eventTdsPdf", eventTdsList);
sessionMapGeneral.put("balanceAmountPdf", balanceAmountList);
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
return SUCCESS;
}
public String generatePdfGeneral() throws Exception {
System.out.println(sessionMapGeneral.get("eventIdPdf"));
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D:\GeneralReports.pdf"));
PdfPTable table = new PdfPTable(11);
table.setSpacingBefore(25);
table.setWidthPercentage(100);
table.setSpacingAfter(25);
PdfPCell c1 = new PdfPCell(new Phrase("Event ID "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Name "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Time"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Venue"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Company Name"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Total Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Received Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cheque/DD Number"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Payment Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event TDS"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Balance Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell( (PdfPCell) sessionMapGeneral.get("eventIdPdf"));
table.addCell("1");
table.addCell("Net Amount");
table.addCell("1");
table.addCell("Service Tax Number: ");
table.addCell("2");
table.addCell("Service Tax @ 0.14 %");
table.addCell("12jh");
table.addCell("123");
table.addCell("safs");
table.addCell("Sbc Tax @0.50%");
document.open();
document.add(table);
document.close();
return "success";
}
@Override
public void setSession(Map<String, Object> map) {
// TODO Auto-generated method stub
sessionMapGeneral = (SessionMap) map;
}
}
我收到这个错误:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.itextpdf.text.pdf.PdfPCell
com.ca.actions.DataForGeneralReportsAction.generatePdfGeneral(DataForGeneralReportsAction.java:346)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
ognl.ASTMethod.getValueBody(ASTMethod.java:90)
ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
ognl.SimpleNode.getValue(SimpleNode.java:258)
ognl.Ognl.getValue(Ognl.java:494)
ognl.Ognl.getValue(Ognl.java:458)
这是错误的:
table.addCell( (PdfPCell) sessionMapGeneral.get("eventIdPdf"));
您有一个 SessionMap
,其中 String
值为键,Object
值为值:
private SessionMap<String, Object> sessionMapGeneral;
您使用键 "eventIdPdf"
添加条目:
sessionMapGeneral.put("eventIdPdf", eventIdList);
eventIdList
是一个 ArrayList
:
List<String> eventIdList = new ArrayList<String>();
您不能将 ArrayList
转换为 PdfPCell
,这正是您在此处尝试执行的操作:
table.addCell( (PdfPCell) sessionMapGeneral.get("eventIdPdf"));
您必须执行以下操作:
PdfPCell cell = new PdfPCell();
List<String> list = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list) {
cell.addElement(new Paragraph(item));
}
table.addCell(cell);
我要做的是制作一些列表,在其中获取值并将其存储在某些特定列表中,因为我想用另一种方法访问这些值,所以我正在创建会话变量并传递通过设置键并使用 get(key) 在另一种方法中访问会话变量中的值,我成功地做到了 so.But 现在我想要的是我想使用列表中的值在另一种方法中制作 pdf。但我做不到 so.Below 是我的代码,请帮我传递制作 pdf 的值。
package com.ca.actions;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import com.ca.database.Database;
import com.ca.pojo.Event;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class DataForGeneralReportsAction extends ActionSupport implements
Preparable, SessionAware {
private List<String> eventsGeneral = new ArrayList<String>();
private List<String> companiesGeneral = new ArrayList<String>();
private SessionMap<String, Object> sessionMapGeneral;
List<String> eventIdList = new ArrayList<String>();
List<String> eventNameList = new ArrayList<String>();
List<String> eventVenueList = new ArrayList<String>();
List<String> eventTimeList = new ArrayList<String>();
List<String> companyNameList = new ArrayList<String>();
List<String> totalAmountList = new ArrayList<String>();
List<String> receivedAmountList = new ArrayList<String>();
List<String> balanceAmountList = new ArrayList<String>();
List<String> eventTdsList = new ArrayList<String>();
List<String> paymentDateList = new ArrayList<String>();
List<String> chequeDdList = new ArrayList<String>();
private String eventGeneral = null;
private String companyGeneral = null;
List<Event> dataForGeneralReports;
public List<String> getEventIdList() {
return eventIdList;
}
public void setEventIdList(List<String> eventIdList) {
this.eventIdList = eventIdList;
}
public List<String> getEventNameList() {
return eventNameList;
}
public void setEventNameList(List<String> eventNameList) {
this.eventNameList = eventNameList;
}
public List<String> getEventVenueList() {
return eventVenueList;
}
public void setEventVenueList(List<String> eventVenueList) {
this.eventVenueList = eventVenueList;
}
public List<String> getEventTimeList() {
return eventTimeList;
}
public void setEventTimeList(List<String> eventTimeList) {
this.eventTimeList = eventTimeList;
}
public List<String> getCompanyNameList() {
return companyNameList;
}
public void setCompanyNameList(List<String> companyNameList) {
this.companyNameList = companyNameList;
}
public List<String> getTotalAmountList() {
return totalAmountList;
}
public void setTotalAmountList(List<String> totalAmountList) {
this.totalAmountList = totalAmountList;
}
public List<String> getReceivedAmountList() {
return receivedAmountList;
}
public void setReceivedAmountList(List<String> receivedAmountList) {
this.receivedAmountList = receivedAmountList;
}
public List<String> getBalanceAmountList() {
return balanceAmountList;
}
public void setBalanceAmountList(List<String> balanceAmountList) {
this.balanceAmountList = balanceAmountList;
}
public List<String> getEventTdsList() {
return eventTdsList;
}
public void setEventTdsList(List<String> eventTdsList) {
this.eventTdsList = eventTdsList;
}
public List<String> getPaymentDateList() {
return paymentDateList;
}
public void setPaymentDateList(List<String> paymentDateList) {
this.paymentDateList = paymentDateList;
}
public List<String> getChequeDdList() {
return chequeDdList;
}
public void setChequeDdList(List<String> chequeDdList) {
this.chequeDdList = chequeDdList;
}
public SessionMap<String, Object> getSessionMapGeneral() {
return sessionMapGeneral;
}
public void setSessionMapGeneral(
SessionMap<String, Object> sessionMapGeneral) {
this.sessionMapGeneral = sessionMapGeneral;
}
public String getEventGeneral() {
return eventGeneral;
}
public void setEventGeneral(String eventGeneral) {
this.eventGeneral = eventGeneral;
}
public String getCompanyGeneral() {
return companyGeneral;
}
public void setCompanyGeneral(String companyGeneral) {
this.companyGeneral = companyGeneral;
}
public List<Event> getDataForGeneralReports() {
return dataForGeneralReports;
}
public void setDataForGeneralReports(List<Event> dataForGeneralReports) {
this.dataForGeneralReports = dataForGeneralReports;
}
public List<String> getEventsGeneral() {
return eventsGeneral;
}
public void setEventsGeneral(List<String> eventsGeneral) {
this.eventsGeneral = eventsGeneral;
}
public List<String> getCompaniesGeneral() {
return companiesGeneral;
}
public void setCompaniesGeneral(List<String> companiesGeneral) {
this.companiesGeneral = companiesGeneral;
}
public DataForGeneralReportsAction() {
// TODO Auto-generated constructor stub
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
Connection con = null;
try {
con = new Database().Get_Connection();
// load companies
PreparedStatement ps = con
.prepareStatement("SELECT DISTINCT company_name FROM event");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
companiesGeneral.add(rs.getString("company_name"));
}
// load events
ps = con.prepareStatement("SELECT DISTINCT event_name FROM event");
rs = ps.executeQuery();
while (rs.next()) {
eventsGeneral.add(rs.getString("event_name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
}
@Override
public String execute() throws Exception {
Connection con = null;
try {
con = new Database().Get_Connection();
// load the table. The first time the table is loaded completely
String sql = "SELECT EVENT_ID, EVENT_NAME, COMPANY_NAME,EVENT_VENUE,TOTAL_AMOUNT,RECEIVED_AMOUNT,EVENT_TDS,BALANCE_AMOUNT,CHEQUE_DD_NO,"
+ "date_format(PAYMENT_DATE,'%d/%m/%Y') as dateAsPayment,EVENT_TIME "
+ "FROM event";
String where = "";
// if instead this action has been called from the JSP page,
// the result is filtered on event and company:
if (eventGeneral != null && companyGeneral != null) {
where = " WHERE event_name = ? AND company_name = ?";
}
// load companies
PreparedStatement ps = con.prepareStatement(sql + where);
if (where.length() > 0) {
ps.setString(1, eventGeneral);
ps.setString(2, companyGeneral);
}
dataForGeneralReports = new ArrayList<Event>();
ResultSet rs = ps.executeQuery();
int i, j = 0;
while (rs.next()) {
dataForGeneralReports.add(new Event(rs.getString("EVENT_ID"),
rs.getString("EVENT_NAME"), rs
.getString("COMPANY_NAME"), rs
.getString("EVENT_VENUE"), rs
.getString("EVENT_TIME"), rs
.getString("TOTAL_AMOUNT"), rs
.getString("RECEIVED_AMOUNT"), rs
.getString("CHEQUE_DD_NO"), rs
.getString("dateAsPayment"), rs
.getString("BALANCE_AMOUNT"), rs
.getString("EVENT_TDS")));
eventIdList.add(rs.getString("EVENT_ID"));
eventNameList.add(rs.getString("EVENT_NAME"));
companyNameList.add(rs.getString("COMPANY_NAME"));
eventVenueList.add(rs.getString("EVENT_VENUE"));
eventTimeList.add(rs.getString("EVENT_TIME"));
totalAmountList.add(rs.getString("TOTAL_AMOUNT"));
receivedAmountList.add(rs.getString("RECEIVED_AMOUNT"));
chequeDdList.add(rs.getString("CHEQUE_DD_NO"));
paymentDateList.add(rs.getString("dateAsPayment"));
eventTdsList.add(rs.getString("EVENT_TDS"));
balanceAmountList.add(rs.getString("BALANCE_AMOUNT"));
}
sessionMapGeneral.put("eventIdPdf", eventIdList);
sessionMapGeneral.put("eventNamePdf", eventNameList);
sessionMapGeneral.put("companyNamePdf", companyNameList);
sessionMapGeneral.put("eventVenuePdf", eventVenueList);
sessionMapGeneral.put("eventTimePdf", eventTimeList);
sessionMapGeneral.put("totalAmountPdf", totalAmountList);
sessionMapGeneral.put("receivedAmountPdf",receivedAmountList);
sessionMapGeneral.put("chequeDdPdf", chequeDdList);
sessionMapGeneral.put("paymentDatePdf",paymentDateList);
sessionMapGeneral.put("eventTdsPdf", eventTdsList);
sessionMapGeneral.put("balanceAmountPdf", balanceAmountList);
} catch (Exception e) {
e.printStackTrace();
} finally {
con.close();
}
return SUCCESS;
}
public String generatePdfGeneral() throws Exception {
System.out.println(sessionMapGeneral.get("eventIdPdf"));
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D:\GeneralReports.pdf"));
PdfPTable table = new PdfPTable(11);
table.setSpacingBefore(25);
table.setWidthPercentage(100);
table.setSpacingAfter(25);
PdfPCell c1 = new PdfPCell(new Phrase("Event ID "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Name "));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Time"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event Venue"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Company Name"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Total Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Received Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cheque/DD Number"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Payment Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Event TDS"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Balance Amount"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell( (PdfPCell) sessionMapGeneral.get("eventIdPdf"));
table.addCell("1");
table.addCell("Net Amount");
table.addCell("1");
table.addCell("Service Tax Number: ");
table.addCell("2");
table.addCell("Service Tax @ 0.14 %");
table.addCell("12jh");
table.addCell("123");
table.addCell("safs");
table.addCell("Sbc Tax @0.50%");
document.open();
document.add(table);
document.close();
return "success";
}
@Override
public void setSession(Map<String, Object> map) {
// TODO Auto-generated method stub
sessionMapGeneral = (SessionMap) map;
}
}
我收到这个错误:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.itextpdf.text.pdf.PdfPCell
com.ca.actions.DataForGeneralReportsAction.generatePdfGeneral(DataForGeneralReportsAction.java:346)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
ognl.ASTMethod.getValueBody(ASTMethod.java:90)
ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
ognl.SimpleNode.getValue(SimpleNode.java:258)
ognl.Ognl.getValue(Ognl.java:494)
ognl.Ognl.getValue(Ognl.java:458)
这是错误的:
table.addCell( (PdfPCell) sessionMapGeneral.get("eventIdPdf"));
您有一个 SessionMap
,其中 String
值为键,Object
值为值:
private SessionMap<String, Object> sessionMapGeneral;
您使用键 "eventIdPdf"
添加条目:
sessionMapGeneral.put("eventIdPdf", eventIdList);
eventIdList
是一个 ArrayList
:
List<String> eventIdList = new ArrayList<String>();
您不能将 ArrayList
转换为 PdfPCell
,这正是您在此处尝试执行的操作:
table.addCell( (PdfPCell) sessionMapGeneral.get("eventIdPdf"));
您必须执行以下操作:
PdfPCell cell = new PdfPCell();
List<String> list = (List<String>) sessionMapGeneral.get("eventIdPdf");
for (String item : list) {
cell.addElement(new Paragraph(item));
}
table.addCell(cell);