主查询中的order by子句的三元运算符

时间:2013-12-31 09:35:05

标签: java sql jasper-reports

我正在开发struts 2 web应用程序,其中
我有一个 JasperReports 报告(jrxml),其中我有主查询

<parameter name="getOrder" class="java.lang.String">
    <defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString>
    <![CDATA[select USER_ID, USER_NAME,CURRENT_ADDRESS,CURRENT_TOWN,CURRENT_POSTAL_CODE,CURRENT_COUNTRY,USER_ADDRESS group by USER_ID, USER_NAME,CURRENT_ADDRESS,CURRENT_TOWN,CURRENT_POSTAL_CODE,CURRENT_COUNTRY,USER_ADDRESS order by $P!{getOrder}, USER_ID]]>
</queryString>

在JSP中,我的屏幕有<select>选项“Order by”,它有5个这样的选项

<select name="FormBean.orderByField" id="orderByField">
                                <option value='1'
                                    <s:if test="FormBean.orderByField == 1">selected="selected"</s:if>>
                                    <fmt:message key="name" />
                                </option>
                                <option value='2'
                                    <s:if test="FormBean.orderByField == 2">selected="selected"</s:if>>
                                    <fmt:message key="current address" />
                                </option>
                                <option value='3'
                                    <s:if test="FormBean.orderByField == 3">selected="selected"</s:if>>
                                    <fmt:message key="user address" />
                                </option>
                                <option value='4'
                                    <s:if test="FormBean.orderByField == 4">selected="selected"</s:if>>
                                    <fmt:message key="postal code" />
                                </option>
                                <option value='5'
                                    <s:if test="FormBean.orderByField == 5">selected="selected"</s:if>>
                                    <fmt:message key="country" />
                                </option>

 </select>

我需要通过select选项中选择的选项获得pdf格式。所以我做的是:我创建了一个HashMap i,我得到了我在select选项中选择的值

jasperParameter.put("getOrder", pdfBean.getManageOrder());

这里pdfBean.getManageOrder()会给我<select>标签中的字符串值,我将在报告中传递

现在,根据“getOrder”中选择的值,我需要用适当的数据替换报告ORDERED的上面的查询字符串中的数据

sql Order by子句取决于像这样选择的值

$P{getOrder}.equals("1")?"USER_NAME":$P{getOrder}.equals("2")?"CURRENT_ADDRESS":$P{getOrder}.equals("3")?"CURRENT_TOWN":$P{getOrder}.equals("4")?"CURRENT_POSTAL_CODE":$P{getOrder}.equals("5")?"COUNTRY":""

如果选择了值3,则查询应该像这样

  <queryString>
    <![CDATA[select USER_ID, USER_NAME,CURRENT_ADDRESS,CURRENT_TOWN,CURRENT_POSTAL_CODE,CURRENT_COUNTRY,USER_ADDRESS group by USER_ID, USER_NAME,CURRENT_ADDRESS,CURRENT_TOWN,CURRENT_POSTAL_CODE,CURRENT_COUNTRY,USER_ADDRESS order by **CURRENT_TOWN**, USER_ID]]>
    </queryString>

我希望在 JR 报告(jrxml)本身完成pdf报告,根据屏幕选择框中选择的值进行订购

1 个答案:

答案 0 :(得分:0)

尝试使用case而不是Ternary运算符:

case $P{getOrder}
WHEN 1 THEN 'USER_NAME' 
WHEN 2 THEN 'CURRENT_ADDRESS' 
WHEN 3 THEN 'CURRENT_TOWN' 
ELSE ''
END