Ternary operator in Spring - spring

My question is similar to this question. Since that question is quite old so thought of posting new question.
I am also writing my expression in following
<property name="to" value="#{ systemProperties['BR']} == '01' ?
${PROPERTY_VALUE_1_FROM_BUNDLE} :
${PROPERTY_VALUE_2_FROM_BUNDLE}" />
When I fetch the value of "to" variable from my bean. Its giving me something like below
01='01'? value1 : value2
Its not parsing my expression in XML itself.
Am I doing anything wrong here?

You are terminating the SpEL too early; it should be...
<property name="to" value="#{ systemProperties['BR'] == '01' ?
'${PROPERTY_VALUE_1_FROM_BUNDLE}' :
'${PROPERTY_VALUE_2_FROM_BUNDLE}' }" />
Note that you also need single quotes around the placeholders so the resolved values are treated as literals.

I have resolved it using below code
ExpressionParser parser = new SpelExpressionParser();
String toMail = parser.parseExpression(to).getValue(String.class);
Had to make little here and there in XML but its responding as I wanted it to. Now I am getting either value in my "to" variable.

Related

Freemarker assigning escaped ${expression} to a variable

I need to assign an escaped ${expression} to a variable in Freemarker
From the question here, it's clear that we can escape the $ sign in this way
${r"${expression}"}
This works perfectly outside Freemarker context, but doesnot working inside. I am trying to do
<#assign x = "${r"${expression}"}">
But getting the following error:
Template inclusion failed:
You can't use "${" here as you are already in FreeMarker-expression-mode. Thus, instead of ${myExpression}, just write myExpression. (${...} is only needed where otherwise static text is expected, i.e, outside FreeMarker tags and ${...}-s.)
What is the way to achieve this? Thanks in advance.
I had to spent some time to figure out the following scenarios to escape ${expression} -
In Freemarker assignment:
<#assign var = r"${expression}">
In html attribute:
Some link
In Freemarker concatenation:
<#assign x = "something&"+r"${expression}"/>
Like this:
<#assign x = r"${expression}">

Spring SpEL: how to write ternary operation in XML config?

In my Spring XML config, I need to set a value to a specific property value depending on the value of another property.
I need something like this:
<bean id="myid" class="myclass">
<property name="myprop"
value="#{${property_a} == 'test-a' ? ${property_b} : 'anothervalue'}"
/>
I want myprop to be set the value of property_b if property_a is equal to "test-a", otherwise myprop must be set to "anothervalue".
property_a and property_b are both defined in my config.properties file.
Is it possible to write such a statement in XML SpEL?
<property name="myprop"
value="#{'${property_a}' == 'test-a' ? '${property_b}' : 'anothervalue' }" />
You have to ensure that the result of properties placeholder resolution is still literal. So, that's why we must wrap ${...} to ''.

Inserting a child node when list is empty (XForms)

My problem is the following :
I usually have those data:
<structures>
<structure id="10">
<code>XXX</code>
</structure>
</structures>
so the table I display (single columns : code) is ok.
But in some cases, the data is the result a a query with no content, so the data is:
<structures/>
resulting in my table not displaying + error.
I am trying to insert, in the case of an empty instance, a single node so that the data would look like:
<structures>
<structure id="0"/>
</structures>
I am trying something like that :
<xforms:action ev:event="xforms-submit-done">
<xforms:insert if="0 = count(instance('{./instance-name}')/root/node())" context="instance('{./instance-name}')/root/node()" origin="xforms:element('structure', '')" />
</xforms:action>
but no node inserted when I look at the data in the inspector in the page.
Any obvious thing I am doing wrong?
There seems to be erros in your XPath if and context expressions:
if="0 = count(instance('{./instance-name}')/root/node())"
context="instance('{./instance-name}')/root/node()"
You are a using curly brackets { and }, I assume to have the behavior of attribute value templates (AVTs). But the if and context expressions are already XPath expressions, so you cannot use AVTs in them. Try instead:
if="0 = count(instance(instance-name)/root/node())"
context="instance(instance-name)/root/node()"
Also, the instance-name path is relative to something which might not be clear when reading or writing the expression. I would suggest using an absolute path for example instance('foo')/instance-name to make things clearer.
You don't provide the structure of the other instances, so I can tell for sure, but you'll expression above suppose that they have the form:
<xf:instance id="foo">
<some-root-element>
<root>
<structure/>
</root>
<some-root-element>
</xf:instance>
I don't know if that's what you intend.
Finally, you could replace count(something) = 0, with empty(something).

Spring config string format

A stupid question on formatting but not able to figure out the correct negate parameter. I've the following string value which needs to be passed as a constructor arg through spring.
private final static String STRING_PATTERN = "\\<.*?>";
In spring config,
<bean class="com.test.TestBean">
<constructor-arg value="\\<.*?>" />
</bean>
As you can see, its not in the correct format. Can anyone please provide a clue ?
Thanks
Since Spring config is an XML file
there is no need to replace \ with \\ as in Java string literals
you need to replace < and > with < and >
So, you get
<constructor-arg value="\<.*?>" />

Cast as integer in ColdFusion; sanitizing variables

I'm rusty at ColdFusion, I've been used to PHP for so long. What I want to do is something like this:
<?php
$id = (isset($_GET['id'])) ? (int)$_GET['id'] : 0;
?>
Basically, check for a url parameter called id and if it exists make sure it's an integer so I can safely use it in database queries. If it ends up zero, that's fine too.
I have:
<cfscript>
if (IsDefined("URL.id") AND IsNumeric(URL.id)) {
id = int(URL.id);
} else {
id = 0;
}
</cfscript>
This is working, but is awfully messy. Is there a better way to do this?
Recent versions of ColdFusion also have a ternary conditional operator:
<cfset id = (structKeyExists(URL, "id") and isNumeric(URL.id)) ? int(URL.id) : 0>
I would use cfparam. I'd also scope explicitly, but that's not necessary. I wouldn't use the IIF() function, because it makes use of evaluate(), which can be problematic, I'd also avoid DE() for the same reason. In this case, it won't be an issue, but I avoid them on general principle in any situation where it's not absolutely necessary. I've been using CF for a few years now, and it hasn't been necessary yet.
<cfparam name="url.id" default="0" />
<cfif isNumeric(url.id)>
<cfset local.id = int(url.id) />
<cfelse>
<cfset local.id = 0 />
</cfif>
To me, the simplest way to ensure your variable is an integer is to wrap the variable in val().
It attempts to parse the string and extract any integer found (at the beginning of the string). If none is found it returns 0.
If TestValue = "234A56?7'", Val(TestValue) returns 234.
If TestValue = "234'5678'9?'", Val(TestValue) returns 234.
If TestValue = "BG234", Val(TestValue) returns the value 0, (not an error).
If TestValue = "0", Val(TestValue) returns the value 0, (not an error).
See http://cfquickdocs.com/cf8/#Val
Apologies for raising an old thread but came up with this same question and found a simple solution that might help others with this issue
NumberFormat(URL.id)
There are also various masks that you can specify in different scenarios
ColdFusion Reference
A formatted number value:
If no mask is specified, returns the value as an integer with a thousands separator.
If the parameter value is "" (an empty string), returns 0.
http://help.adobe.com/livedocs/coldfusion/8/htmldocs/help.html?content=functions_m-r_08.html
You might also look into cfparam.
<cftry>
<cfparam name="url.id" default="0" type="integer">
<cfcatch>
<!--- log? etc --->
<cfset url.id = 0>
</cfcatch>
</cftry>
You can use IIF. It's cool.
<cfset id = IIf(IsDefined("URL.id") AND Int(URL.id), Int(URL.id), DE("0"))>

Resources