Use eclipse to display the Crystal Report parameters on JSP (6)

Source: Internet
Author: User

It took me three days.

I. Submit PARAMETERS TO THE CRYSTAL REPORT

Open the report file. Rpt. In field resource manager, right-click parameter-> Create

In the options attribute below, you can set the parameter name, type, prompt text, whether multiple values are allowed, value range type (if the string is used for discretization, date or number with range)

Note: The preceding parameters are compared. If the parameter is used to correspond to a field, the type must be the same as that of the field.

You can create a Value List in the value attribute, click it, and then click the button on the right to enter the Value List edit box; Whether to display only instructions when prompted; Edit mask, you can specify a certain format for the parameter; minimum length; maximum length;

Now you can create a viewer JSP for this report and add the viewer tag.

Note that the page encoding for this JSP is set to UTF-8.

2. required parameters and related objects

1.Required Java classes and packages

Com. crystaldecisions. SDK. occa. Report. Data .*

Com. crystaldecisions. Reports. SDK. datadefcontroller

2.Create a fields object that stores parameter fields. You can add, delete, and search for parameter objects through the fields object.

Fields = new fields ();

3. parameterfield Parameter Field object.

Parameterfield paramfield1 = new parameterfield ();

4.Create a values object for each parameter field you want to set. The values object is used to store the parameterfielddiscretevalue object or the parameterfieldrangevalue object.

Values newvals1 = new values ();

5.Discrete value object parameterfielddiscretevalue object

Parameterfielddiscretevalue newdiscval1 = new parameterfielddiscretevalue ();

Parameterfielddiscretevalue [] newdiscvals = new parameterfielddiscretevalue [];

Parameterfieldrangevalue object

Parameterfieldrangevalue newrangeval1 = new parameterfieldrangevalue ();

Parameterfieldrangevalue [] newrangevals = new parameterfieldrangevalue [];

Their approximate relationship: One fields-multiple parameterfields

One parameterfield-> one values

One values-"multiple parameterfielddiscretevalue or parameterfieldrangevalue

3. Set parameters in the background

1.Parameter management is controlled by the report datadefcontroller.

Datadefcontroller = new datadefcontroller ();

For the main report datadefcontroller = reportclientdoc. getdatadefcontroller ();

For subreports

Datadefcontroller = clientdoc. getsubreportcontroller (). getsubreport (reportname). getdatadefcontroller ();

2.The parameter settings are obtained through idatadefinition.

Idatadefinition datadefinition = datadefcontroller. getdatadefinition ();

For example, fields = datadefcontroller. getdatadefinition (). getparameterfields ();

Parameters are controlled through parameterfieldcontroller.

Parameterfieldcontroller paramfieldcontroller = datadefcontroller. getparameterfieldcontroller ();

For example, set a single value for the parameter fields of the main report.

Paramfieldcontroller. setcurrentvalue ("", "stringparam", new string ("hello"); // string type
Paramfieldcontroller. setcurrentvalue ("", "booleanparam", new Boolean (true); // Boolean Type
Paramfieldcontroller. setcurrentvalue ("", "currencyparam", new double (123.45); // currency type
Paramfieldcontroller. setcurrentvalue ("", "numberparam", new INTEGER (123); // numeric type

Set multiple discrete values for a parameter field

Object [] multivals = {"string1", "string2", "string3"}; // you must specify the type of the parameter field.
Paramfieldcontroller. setcurrentvalues ("", "stringmultiparam", multivals );

3.Obtain Fields

Fields = datadefcontroller. getdatadefinition (). getparameterfields ();

4.You can use fields to obtain the created parameter fields by finding their names.

Parameterfield paramfield = (parameterfield) datadefcontroller. getdatadefinition (). getparameterfields (). findfield (parametername, fielddisplaynametype. fieldname, locale. getdefault ());

Seven basic types of parameter fields
Boolean, double, integer, String, date, date)

The parameter value must be consistent with the parameter type.

5.For a new parameter field, you must set the Report Name of the parameter field to the report name associated with the parameter. If the parameter field is used in the main report, a blank string ("") is used (""). Use the setreportname method to set the report name.

Paramfield1.setname ("country"); // set the name

Paramfield1.setreportname (""); // if it is "", it is used for the primary report. If it is a subreport, you must set the report name.

If the parameter already exists in RPT, you do not need to set the name and report name or add it to fields.

Newdiscval1.setvalue ("China"); // sets the discrete value, which will be added to the parameter value list later.

Newdiscval1.setdescription ("the country is China."); // This does not matter

Add the parameter field value to the values set object.

Newvals1.add (newdiscval1); // you can add multiple

Set the values set for the parameter fields.

Pfield1.setcurrentvalues (vals1 );

Add each parameter field to the fields object. Fields objects can now be used in the viewer

Fields. Add (pfield1); // not required for a non-New Parameter

The following is the focus. It was simple, but it tortured me for three days.

6.Modify the parameter field discrete value list or range value list on the JSP page of the report.

First, use the paramfield object to get it. How to get it first is not mentioned here

Discrete value to be added

Parameterfielddiscretevalue newdiscvalue = new parameterfielddiscretevalue ();
Newdiscvalue. setvalue (newvalue );

Obtain the original values set and clone the values set to a new values set. I used to use paramfield. getvalues (). I don't know whether there is an error or something in JRC. This method is useless. paramfield. getdefavaluvalues (). Only in this way can the values set of the parameter object be truly obtained.

Values newvals = (values) paramfield. getdefavaluvalues (). Clone (true );

Add multiple discrete values to the original discrete values.

Newvals. Add (newdiscvalue );

Set a new values set for the parameter fields. Paramfield. setcurrentvalues (newvals) is used here, and it is not supported. Use the following paramfield. setdefavaluvalues (newvals );

Paramfield. setdefavaluvalues (newvals );

7.Disable User prompts and automatically use the set parameter field values. This is particularly useful in the following situations: the set parameter field value is unknown to the user (for example, a parameter generated by the system ).

Use Boolean multivalue = paramfield. getallowmultivalue (); to determine whether the report allows multiple values.

Multi-value is not allowed:

Clientdoc. getdatadefcontroller (). getparameterfieldcontroller (). setcurrentvalue (reportname, parametername, newvalue );

For the primary report, The reportname is ""; otherwise, the subreport name is used.

When multiple values are allowed:

① If you do not consider the original discrete value list

String [] newvalues = new string [] {"China", "USA ",..........};

Clientdoc. getdatadefcontroller (). getparameterfieldcontroller (). setcurrentvalues (reportname, parametername, newvalues );

 

② Add new discrete values based on the original discrete value list and set

Datadefcontroller = NULL;
If (reportname. Equals (""))
Datadefcontroller = clientdoc. getdatadefcontroller ();
Else
Datadefcontroller = clientdoc. getsubreportcontroller (). getsubreport (reportname). getdatadefcontroller ();

Parameterfield paramfield = (parameterfield) datadefcontroller. getdatadefinition (). getparameterfields (). findfield (parametername, fielddisplaynametype. fieldname, locale. getdefault ());

Parameterfielddiscretevalue newdiscvalue = new parameterfielddiscretevalue ();
Newdiscvalue. setvalue (newvalue );

Values newvals = (values) paramfield. getdefavaluvalues (). Clone (true );

Newvals. Add (newdiscvalue );

Clientdoc. getdatadefcontroller (). getparameterfieldcontroller (). setcurrentvalue (reportname, parametername, newvals );

Viewer settings

Crystalreportpageviewer. setparameterfields (fields); // It doesn't matter if you don't need it.

Crystalreportpageviewer. setenableparameterprompt (false );

After the parameters of the report are set, call the refresh method of the viewer to apply the new parameters.

If (session. getattribute ("refresh") = NULL ){
Crystalreportpageviewer. Refresh ();
Session. setattribute ("refresh", "true ");
}
Call the processhttprequest method to start this viewer in the current browser window.

Crystalreportpageviewer. processhttprequest (request, response, getservletconfig (). getservletcontext (), null );

Do not set the refresh button in the viewer. If you press it, parameter settings will be lost. Page encoding UTF-8. If not, Tomcat profect-> reload this context

 

Alas, I just wrote a lot of text, but it's gone after just a few clicks. The recent blog system is unstable.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.