手动变量增量 [英] manual variable increment

查看:141
本文介绍了手动变量增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到在报告生成过程中创建变量并修改它的方法。

I can't find the way to just create a variable and modify it during report generation.

我已经声明了一个整数变量:

I have declared an integer variable:

<variable name="my_counter" class="java.lang.Integer" calculation="System">
    <initialValueExpression><![CDATA[new Integer(0)]]></initialValueExpression>
</variable>

这似乎有效,我可以毫无问题地打印它的价值。但是我想在报告生成期间增加该值:当XML(报告的数据源)包含某些参数时,我想增加值(my_counter ++)。

That seems to work, and I can print it's value without problems. But I want to increment that value during report generation: when the XML (the datasource for the report) contains certain parameters I want to increase the value (my_counter++).

我想要实现的目标,在伪代码中:

<textField printWhenExpression="$P{BANANAS}!=null"> ($V{my_counter}++)+" The XML contains <bananas>" </textField>
<textField printWhenExpression="$P{APPLES}!=null"> ($V{my_counter}++)+" The XML contains <apples>" </textField>
<textField printWhenExpression="$P{GRAPES}!=null"> ($V{my_counter}++)+" The XML contains <grapes>" </textField>
<textField printWhenExpression="$P{ORANGES}!=null"> ($V{my_counter}++)+" The XML contains <oranges>" </textField>

包含香蕉,苹果和橙子的XML文件的预期结果 be:

1. The XML contains <bananas>
2. The XML contains <apples>
3. The XML contains <oranges>

我试过了,但目前的结果看起来更像是这样的:

I've tried that, but the current result looks more like this:

0. The XML contains <bananas>
0. The XML contains <apples>
0. The XML contains <oranges>

所以似乎变量 my_counter 是没有被修改。为什么?如何修改每个显示的textField的值?

So it seems that the variable my_counter is not being modified. Why? How can I modify it's value for each displayed textField?

推荐答案

您必须使用可变值持有者而不是 java.lang.Integer 表示变量。例如, java.util.concurrent.atomic.AtomicInteger 会做(你不需要原子性,你也可以使用Commons Lang的MutableInt)。

You'll have to use a mutable value holder instead of java.lang.Integer for the variable. java.util.concurrent.atomic.AtomicInteger would do for instance (you don't need atomicity, you can also go with MutableInt from Commons Lang).

一旦将变量声明为AtomicInteger,就可以在表达式中执行 $ V {my_counter} .getAndIncrement()

Once you declare your variable as AtomicInteger, you can do $V{my_counter}.getAndIncrement() in your expressions.

这篇关于手动变量增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆