Skip to Content
0
Former Member
May 10, 2012 at 08:58 AM

Cannot pass parameter in JSF

291 Views

I am writing a JSF page to get user input, and pass that input to Crystal Report (Crystal Report for Eclipse 2.0). I don't know why the parameterFields cannot load my parameter. Please help me!!!

My First page (rptDemo.xhtml):

  
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4. xmlns:h="http://java.sun.com/jsf/html"
  5. xmlns:f="http://java.sun.com/jsf/core">
  6. <h:head>
  7. <title>Crystal Report Demo</title>
  8. </h:head>
  9. <h:body>
  10. <h1>Crystal Report for Eclipse 2.0 Demo</h1>
  11. <h2>Using JSF 2.0 and JSF 1.2 to generate report</h2>
  12. <h3>This input/select page use JSF 2.0, result page use JSF 1.2 </h3>
  13. <f:view>
  14. <h:form>
  15. <table border="1">
  16. <tr>
  17. <td>No parameter passing to report</td>
  18. <td colspan="2"><h:commandButton value="Show report 1" action="#{reportBean.show1}"/> can show report normally</td>
  19. </tr>
  20. <tr>
  21. <td>Passing parameter to report<br/>(cannot pass parameter)</td>
  22. <td><h:inputText value="#{reportBean.inputText}"/></td>
  23. <td><h:commandButton value="Show report 2" action="#{reportBean.show2}"/></td>
  24. </tr>
  25. </table>
  26. </h:form>
  27. </f:view>
  28. </h:body>
  29. </html>

My second page (result page) for "Show Report 1 (rpt1.jsp)", this can run successfully:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>  <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>  <%@ taglib uri="http://www.businessobjects.com/jsf/crystalreportsviewers" prefix="bocrv" %>          <head><title>View Report 1</title>  </head>  <body>  <f:view>  <h:form>  <bocrv:reportPageViewer reportSource="#{reportBean.reportSource1}"  />   </h:form>  </f:view>  <body>  </html> 

My third page (result page) for "Show Report 2 (rpt2.jsp)", this has error, parameterFields seem cannot load parameter:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>  <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>  <%@ taglib uri="http://www.businessobjects.com/jsf/crystalreportsviewers" prefix="bocrv" %>          <head><title>View Report 2</title>  </head>  <body>  <f:view>  <h:form>  <bocrv:reportPageViewer reportSource="#{reportBean.reportSource2}" allowParameterPrompting="false"  parameterFields="#{reportBean.parameterFields}" />   </h:form>  </f:view>  <body>  </html> 

My report bean is:

  
  
  1. package crDemo;
  2. import javax.faces.bean.ManagedBean;
  3. import com.crystaldecisions.reports.reportengineinterface.JPEReportSourceFactory;
  4. import com.crystaldecisions.sdk.occa.report.data.Fields;
  5. import com.crystaldecisions.sdk.occa.report.data.ParameterField;
  6. import com.crystaldecisions.sdk.occa.report.data.ParameterFieldDiscreteValue;
  7. import com.crystaldecisions.sdk.occa.report.data.Values;
  8. import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
  9. import com.crystaldecisions.sdk.occa.report.reportsource.IReportSourceFactory2;
  10. @ManagedBean
  11. public class ReportBean {
  12. private Object reportSource1 = null;
  13. private Object reportSource2 = null;
  14. private String inputText = "";
  15. private Fields<ParameterField> parameterFields = new Fields<ParameterField>();
  16. public Object getReportSource1() {
  17. return reportSource1;
  18. }
  19. public void setReportSource1(Object reportSource1) {
  20. this.reportSource1 = reportSource1;
  21. }
  22. public Object getReportSource2() {
  23. return reportSource2;
  24. }
  25. public void setReportSource2(Object reportSource2) {
  26. this.reportSource2 = reportSource2;
  27. }
  28. public String getInputText() {
  29. return inputText;
  30. }
  31. public void setInputText(String inputText) {
  32. this.inputText = inputText;
  33. }
  34. public Fields<ParameterField> getParameterFields() {
  35. return parameterFields;
  36. }
  37. public void setParameterFields(Fields<ParameterField> parameterFields) {
  38. this.parameterFields = parameterFields;
  39. }
  40. public String show1() {
  41. try {
  42. // Instantiate the report object
  43. String reportLocation;
  44. reportLocation = "rpt/demo1.rpt";
  45. IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory();
  46. reportSource1 = rptSrcFactory.createReportSource(reportLocation, java.util.Locale.getDefault() );
  47. } catch (ReportSDKException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51. return "rpt1";
  52. }
  53. public String show2() {
  54. try {
  55. // Instantiate the report object
  56. String reportLocation;
  57. reportLocation = "rpt/demo2.rpt";
  58. IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory();
  59. reportSource2 = rptSrcFactory.createReportSource(reportLocation, java.util.Locale.getDefault() );
  60. // Clear previous parameter
  61. parameterFields.clear();
  62. //Create a ParameterField object for each field that you wish to set.
  63. ParameterField paramFieldOne = new ParameterField();
  64. //You must set the report name. Set the report name to an empty string if your report does not contain a subreport; otherwise, the report name will be the name of the subreport
  65. paramFieldOne.setReportName("");
  66. //Create a Values object and a ParameterFieldDiscreteValue object for each parameter field you wish to set.
  67. //If a ranged value is being set, a ParameterFieldRangeValue object should be used instead of the discrete value object.
  68. Values valueOne = new Values();
  69. ParameterFieldDiscreteValue paramFieldDVOne = new ParameterFieldDiscreteValue();
  70. //----------- Initialize the parameter fields ----------
  71. //Set the name and value for each parameter field that is added.
  72. //Values for parameter fields are represented by a ParameterFieldDiscreteValue or ParameterFieldRangeValue object.
  73. System.out.println("Input parameter = " + inputText);
  74. paramFieldOne.setName("paramOne"); // "paramOne" is parameter name in my Crystal Report file (demo2.rpt)
  75. paramFieldDVOne.setValue(inputText);
  76. //Add the parameter field values to the Values collection object.
  77. valueOne.add(paramFieldDVOne);
  78. //Set the current Values collection for each parameter field.
  79. paramFieldOne.setCurrentValues(valueOne);
  80. //Add each parameter field to the Fields collection.
  81. //The Fields object is now ready to be used with the viewer.
  82. parameterFields.add(paramFieldOne);
  83. } catch (ReportSDKException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. return "rpt2";
  88. }
  89. }

Here is runtime result:

https://public.bay.livefilestore.com/y1pigYkVz4Js_QEKhde3sLBYPuh4V2iGWCboJG3omrD6w5vwl1gOWhiMpfMcPFMzfENu7tLBpK-m1wwWb0mTYv7sg/run.png?psid=1

After enter some value, and click "Show report 2"

https://public.bay.livefilestore.com/y1pHsxtGjMgSla_sZlihB6Y7xB6kOyhcOMSwND5xgin5qV1sGw85K_SBJUFJUGt_OlOm7F2fwZ1FO_qVA25cxpmFA/Missing.png?psid=1