พอดีใช้ IReport 3.6.1 ในการสร้างรายงานครับ แล้วเขียนเป็น Servletให้มันไปแสดงเป็น PDF บน Browser ครับ ส่วน Server เป็น WebSphere ครับ
(เนื้อหาในรายงานเป็นภาษาไทยนะครับ ใช้ font THsarabun) นี่ส่วนของของโค้ดนะครับ
private void performTask(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
String reportFileName = "fine_report";
System.out.println("________________Servlet");
String PerNo = "";
TableOfZshpaCommunication EmpInfo_Comm = new TableOfZshpaCommunication();
TableOfZshpaEmployeeInfo EmpInfo_Info = new TableOfZshpaEmployeeInfo();
TableOfZshpaPosition EmpInfo_Pos = new TableOfZshpaPosition();
Bapiret2 bp2 = new Bapiret2();
fineModel model = new fineModel();
ArrayList<String> rs_Arr = new ArrayList<String>
);
String myCookie = (String) session.getAttribute("MySAPTicket");
System.out.println("myCookie : "+myCookie);
String iPersonalNumber = request.getParameter("perNo");
java.util.Locale dlocale = new java.util.Locale("en", "US");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd", dlocale);
String ToDay = df.format(new java.util.GregorianCalendar().getTime());
String iBegda = ToDay;
String iEndda = ToDay.toString();
String endpointUrl = "";
eperProperties prop = eperProperties.getInstance();
endpointUrl = prop.getURLEMPInfo();
System.out.println("__endpointURL = "+endpointUrl);
try {
ZWS_HPA_I001_EMP_INFOProxy proxy = new ZWS_HPA_I001_EMP_INFOProxy();
proxy._getDescriptor().setEndpoint(endpointUrl);
Hashtable<String, String> ht = new Hashtable<String, String>
);
ht.put("MYSAPSSO2", myCookie);
proxy._getDescriptor().setCookie(ht);
// Init Table
Holder<TableOfZshpaCommunication> etCommunication = new Holder<TableOfZshpaCommunication>
);
TableOfZshpaCommunication zsComm = new TableOfZshpaCommunication();
etCommunication.value = zsComm;
Holder<TableOfZshpaEmployeeInfo> etEmployeeInfo = new Holder<TableOfZshpaEmployeeInfo>
);
TableOfZshpaEmployeeInfo zsEmpInfo = new TableOfZshpaEmployeeInfo();
etEmployeeInfo.value = zsEmpInfo;
Holder<TableOfZshpaPosition> etPosition = new Holder<TableOfZshpaPosition>
);
TableOfZshpaPosition zsPosition = new TableOfZshpaPosition();
etPosition.value = zsPosition;
Holder<Bapiret2> eReturn = new Holder<Bapiret2>
);
Bapiret2 bapi2 = new Bapiret2();
eReturn.value = bapi2;
proxy.zHpaI001EmployeeInfo(etCommunication, etEmployeeInfo, etPosition, iBegda, iEndda, iPersonalNumber, eReturn);
EmpInfo_Comm = etCommunication.value;
EmpInfo_Info = etEmployeeInfo.value;
EmpInfo_Pos = etPosition.value;
bp2 = eReturn.value;
for (int i = 0; i < EmpInfo_Info.getItem().size(); i++) {
model.setName_th(isNull(EmpInfo_Info.getItem().get(i).getNameTh().toString()));
model.setDot(isNull(EmpInfo_Info.getItem().get(i).getDot().toString()));
System.out.println("__NameTh : "+model.getName_th());
System.out.println("__Dot : "+model.getDot());
rs_Arr.add(model.getName_th());
rs_Arr.add(model.getDot());
}
for (int i = 0; i < EmpInfo_Pos.getItem().size(); i++) {
model.setPosition(isNull(EmpInfo_Pos.getItem().get(i).getPositionName().toString()));
model.setFunction_code(isNull(EmpInfo_Pos.getItem().get(i).getFunctionCode()));
System.out.println("__Position : "+model.getPosition());
System.out.println("__FunctionCode : "+model.getFunction_code());
rs_Arr.add(model.getPosition());
rs_Arr.add(model.getFunction_code());
}
Map<String,String> parameters = new HashMap<String,String>
);
parameters.put("authorName", rs_Arr.get(0));
parameters.put("endDate", rs_Arr.get(1));
parameters.put("posName", rs_Arr.get(2));
parameters.put("posNumber", rs_Arr.get(3));
//hard code
parameters.put("startDate", "15/08/2007");
parameters.put("totalDate", "DD");
parameters.put("incDate", "8");
parameters.put("decDate", "FF");
parameters.put("missDate", "FF");
Set<Entry<String,String>> hashSet=parameters.entrySet();
for(Entry<String,String> entry:hashSet ) {
System.out.println("__Key="+entry.getKey()+", __Value="+entry.getValue());
}
JasperReport jasperReport = getCompiledReport(reportFileName);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
response.sendRedirect(request.getContextPath() + "/servlets/report/pdf");
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}
private JasperReport getCompiledReport(String fileName) throws JRException {
File reportFile = new File(getServletContext().getRealPath("/reports/" + fileName + ".jasper"));
// If compiled file is not found, then
if (!reportFile.exists()) {
JasperCompileManager.compileReportToFile(getServletContext()
.getRealPath("/reports/" + fileName + ".xml"));
}
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
return jasperReport;
}
public void init() throws ServletException {
super.init();
ServletContext context = getServletContext();
// Set class path for compiling XML templates
System.setProperty(JRProperties.COMPILER_CLASSPATH,
context.getRealPath("WEB-INF/lib/jasperreports-3.6.1.jar")
+ System.getProperty("path.separator")
+ context.getRealPath("WEB-INF/classes/"));
System.setProperty(JRProperties.COMPILER_TEMP_DIR,
context.getRealPath("/reports/"));
}
ส่วนอันนี้ Error msg นะครับ
[8/14/15 11:39:51:394 ICT] 00000063 servlet E com.ibm.ws.webcontainer.servlet.ServletWrapper service Uncaught service() exception thrown by servlet PrintFineReportServlet: java.lang.NoClassDefFoundError: org.springframework.core.io.Resource
at java.lang.J9VMInternals.verifyImpl(Native Method)
at java.lang.J9VMInternals.verify(J9VMInternals.java:85)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:162)
at java.lang.Class.forNameImpl(Native Method)
at java.lang.Class.forName(Class.java:170)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:157)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:115)
at net.sf.jasperreports.engine.util.ClassUtils.instantiateClass(ClassUtils.java:53)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.instantiateRegistry(DefaultExtensionsRegistry.java:237)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:214)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:162)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getRegistries(DefaultExtensionsRegistry.java:133)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:105)
at net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>
JRStyledTextParser.java:83)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:228)
at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>
JRBaseFiller.java:121)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>
JRVerticalFiller.java:88)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>
JRVerticalFiller.java:103)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>
JRVerticalFiller.java:61)
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:179)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:148)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:464)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:861)
at tg.pa.epersonneladmin.servlets.PrintFineReportServlet.performTask(PrintFineReportServlet.java:254)
at tg.pa.epersonneladmin.servlets.PrintFineReportServlet.doGet(PrintFineReportServlet.java:83)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1214)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1027)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3703)
at com.ibm.ws.webcontainer.webapp.WebGro
มีปัญหากับ IReport 3.6.1 ครับ รบกวนผู้รู้ช่วยตอบทีครับ
(เนื้อหาในรายงานเป็นภาษาไทยนะครับ ใช้ font THsarabun) นี่ส่วนของของโค้ดนะครับ
private void performTask(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
String reportFileName = "fine_report";
System.out.println("________________Servlet");
String PerNo = "";
TableOfZshpaCommunication EmpInfo_Comm = new TableOfZshpaCommunication();
TableOfZshpaEmployeeInfo EmpInfo_Info = new TableOfZshpaEmployeeInfo();
TableOfZshpaPosition EmpInfo_Pos = new TableOfZshpaPosition();
Bapiret2 bp2 = new Bapiret2();
fineModel model = new fineModel();
ArrayList<String> rs_Arr = new ArrayList<String>);
String myCookie = (String) session.getAttribute("MySAPTicket");
System.out.println("myCookie : "+myCookie);
String iPersonalNumber = request.getParameter("perNo");
java.util.Locale dlocale = new java.util.Locale("en", "US");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd", dlocale);
String ToDay = df.format(new java.util.GregorianCalendar().getTime());
String iBegda = ToDay;
String iEndda = ToDay.toString();
String endpointUrl = "";
eperProperties prop = eperProperties.getInstance();
endpointUrl = prop.getURLEMPInfo();
System.out.println("__endpointURL = "+endpointUrl);
try {
ZWS_HPA_I001_EMP_INFOProxy proxy = new ZWS_HPA_I001_EMP_INFOProxy();
proxy._getDescriptor().setEndpoint(endpointUrl);
Hashtable<String, String> ht = new Hashtable<String, String>);
ht.put("MYSAPSSO2", myCookie);
proxy._getDescriptor().setCookie(ht);
// Init Table
Holder<TableOfZshpaCommunication> etCommunication = new Holder<TableOfZshpaCommunication>);
TableOfZshpaCommunication zsComm = new TableOfZshpaCommunication();
etCommunication.value = zsComm;
Holder<TableOfZshpaEmployeeInfo> etEmployeeInfo = new Holder<TableOfZshpaEmployeeInfo>);
TableOfZshpaEmployeeInfo zsEmpInfo = new TableOfZshpaEmployeeInfo();
etEmployeeInfo.value = zsEmpInfo;
Holder<TableOfZshpaPosition> etPosition = new Holder<TableOfZshpaPosition>);
TableOfZshpaPosition zsPosition = new TableOfZshpaPosition();
etPosition.value = zsPosition;
Holder<Bapiret2> eReturn = new Holder<Bapiret2>);
Bapiret2 bapi2 = new Bapiret2();
eReturn.value = bapi2;
proxy.zHpaI001EmployeeInfo(etCommunication, etEmployeeInfo, etPosition, iBegda, iEndda, iPersonalNumber, eReturn);
EmpInfo_Comm = etCommunication.value;
EmpInfo_Info = etEmployeeInfo.value;
EmpInfo_Pos = etPosition.value;
bp2 = eReturn.value;
for (int i = 0; i < EmpInfo_Info.getItem().size(); i++) {
model.setName_th(isNull(EmpInfo_Info.getItem().get(i).getNameTh().toString()));
model.setDot(isNull(EmpInfo_Info.getItem().get(i).getDot().toString()));
System.out.println("__NameTh : "+model.getName_th());
System.out.println("__Dot : "+model.getDot());
rs_Arr.add(model.getName_th());
rs_Arr.add(model.getDot());
}
for (int i = 0; i < EmpInfo_Pos.getItem().size(); i++) {
model.setPosition(isNull(EmpInfo_Pos.getItem().get(i).getPositionName().toString()));
model.setFunction_code(isNull(EmpInfo_Pos.getItem().get(i).getFunctionCode()));
System.out.println("__Position : "+model.getPosition());
System.out.println("__FunctionCode : "+model.getFunction_code());
rs_Arr.add(model.getPosition());
rs_Arr.add(model.getFunction_code());
}
Map<String,String> parameters = new HashMap<String,String>);
parameters.put("authorName", rs_Arr.get(0));
parameters.put("endDate", rs_Arr.get(1));
parameters.put("posName", rs_Arr.get(2));
parameters.put("posNumber", rs_Arr.get(3));
//hard code
parameters.put("startDate", "15/08/2007");
parameters.put("totalDate", "DD");
parameters.put("incDate", "8");
parameters.put("decDate", "FF");
parameters.put("missDate", "FF");
Set<Entry<String,String>> hashSet=parameters.entrySet();
for(Entry<String,String> entry:hashSet ) {
System.out.println("__Key="+entry.getKey()+", __Value="+entry.getValue());
}
JasperReport jasperReport = getCompiledReport(reportFileName);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
response.sendRedirect(request.getContextPath() + "/servlets/report/pdf");
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}
private JasperReport getCompiledReport(String fileName) throws JRException {
File reportFile = new File(getServletContext().getRealPath("/reports/" + fileName + ".jasper"));
// If compiled file is not found, then
if (!reportFile.exists()) {
JasperCompileManager.compileReportToFile(getServletContext()
.getRealPath("/reports/" + fileName + ".xml"));
}
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
return jasperReport;
}
public void init() throws ServletException {
super.init();
ServletContext context = getServletContext();
// Set class path for compiling XML templates
System.setProperty(JRProperties.COMPILER_CLASSPATH,
context.getRealPath("WEB-INF/lib/jasperreports-3.6.1.jar")
+ System.getProperty("path.separator")
+ context.getRealPath("WEB-INF/classes/"));
System.setProperty(JRProperties.COMPILER_TEMP_DIR,
context.getRealPath("/reports/"));
}
ส่วนอันนี้ Error msg นะครับ
[8/14/15 11:39:51:394 ICT] 00000063 servlet E com.ibm.ws.webcontainer.servlet.ServletWrapper service Uncaught service() exception thrown by servlet PrintFineReportServlet: java.lang.NoClassDefFoundError: org.springframework.core.io.Resource
at java.lang.J9VMInternals.verifyImpl(Native Method)
at java.lang.J9VMInternals.verify(J9VMInternals.java:85)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:162)
at java.lang.Class.forNameImpl(Native Method)
at java.lang.Class.forName(Class.java:170)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:157)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:115)
at net.sf.jasperreports.engine.util.ClassUtils.instantiateClass(ClassUtils.java:53)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.instantiateRegistry(DefaultExtensionsRegistry.java:237)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:214)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:162)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getRegistries(DefaultExtensionsRegistry.java:133)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:105)
at net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>JRStyledTextParser.java:83)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:228)
at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>JRBaseFiller.java:121)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>JRVerticalFiller.java:88)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>JRVerticalFiller.java:103)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>JRVerticalFiller.java:61)
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:179)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:148)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:464)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:861)
at tg.pa.epersonneladmin.servlets.PrintFineReportServlet.performTask(PrintFineReportServlet.java:254)
at tg.pa.epersonneladmin.servlets.PrintFineReportServlet.doGet(PrintFineReportServlet.java:83)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1214)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1027)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3703)
at com.ibm.ws.webcontainer.webapp.WebGro