I have a MyMethod class that implements TemplateMethodModel. I need to pass a HashMap to the args of the exec function.
How can I do that from a ftl template ?
FYI I have tried ${myMethod({"key":"value"})} but inside curly brackets are not accepted..
Thanks in advance for your help.
You need to use TemplateMethodModelEx, as TemplateMethodModel converts the arguments to string (it's a legacy...). (You still won't get a HashMap, but a TemplateHashModelEx.)
Related
I want to get a variable's class type in freemarker, used var.class.simpleName;
but if var is a Map, freemarker will process class as a key to find value in var.
it throw exception. how can I do this ? thanks for any suggestion.
First I have to ask why do you need that, because FreeMarker templates aren't supposed to know even if var is Map at all. Maybe your data-model is not what the template needs.
Anyway, for now, I would write a custom TemplateMethodModelEx for this purpose, something that you can use like ${classOf(var)}. Inside the TemplateMethodModelEx implementation you will receive a TemplateModel as the argument value, and then you can check if it's an AdapterTemplateModel, and if so you can get back the original object and get its class. (If it's not a AdapterTemplateModel, then it perhaps isn't even a wrapped Java object, so it doesn't make sense to ask what the class of the original object is.) However, the DefaultObjectWrapper with incompatibleImprovements set to less than 2.3.22 doesn't give AdapterTemplateModel to wrapped Map-s... so in 2.3.21 you will still have to use BeansWrapper, but you can at least set simpleMapWrapper to true.
In 2.3.22 it will be actually possible to write ${var?api.class}... you might use the nightly build. Though it only supposed to solve the problem where you can't access business methods because the primary type of the business class is Map.
I have the following in a Spring bean:
#Value("${myValue}")
private String value;
The value is correctly injected. However, the variable needs to be optional, it is passed in as a command line parameter (which is then added to the Spring context using a SimpleCommandLinePropertySource), and this argument will not always exist.
I have tried both the following in order to provide a default value:
#Value("${myValue:}")
#Value("${myValue:DEFAULT}")
but in each case, the default argument after the colon is injected even when there is an actual value - this appears override what Spring should inject.
What is the correct way to specify that #Value is not required?
Thanks
What is the correct way to specify that #Value is not required?
Working on the assumption that by 'not required' you mean null then...
You have correctly noted that you can supply a default value to the right of a : character. Your example was #Value("${myValue:DEFAULT}").
You are not limited to plain strings as default values. You can use SPEL expressions, and a simple SPEL expression to return null is:
#Value("${myValue:#{null}}")
If you are using Java 8, you can take advantage of its java.util.Optional class.
You just have to declare the variable following this way:
#Value("${myValue:#{null}}")
private Optional<String> value;
Then, you can check whether the value is defined or not in a nicer way:
if (value.isPresent()) {
// do something cool
}
Hope it helps!
If you want to make the configuration property optional just pass an empty string like this:
#Value("${app.optional.value:}")
I guess you are you using multiple context:property-placeholder/ declarations?
If so, this is a known issue since 2012, but not fixed, apparently due to both lack of interest and no clean way of fixing it. See https://github.com/spring-projects/spring-framework/issues/14623 for discussion and some ways to work around it. It's explained in an understandable way by http://www.michelschudel.nl/wp/2017/01/25/beware-of-multiple-spring-propertyplaceholderconfigurers-and-default-values/
I need to add a method inside this add(null) method but the method has a string argument like the following
private double mymethod(String writing) // This is the method which must replace
// the null value inside the add(null) method
but since it has a String argument this gives an error! what could i do in order to fix this problem?
From what you have I think that features is a Collection of Doubles - is that right?
And what you want to do is the following:
features.add(mymethod("some string"));
If thats the case I dont see the problem I'm afraid - where is the error being thrown?
Provided your mymethod returns a valid double I cant see what else would cause you a problem here.
Need to make certain Ruby strings in my program to be immutable. What is the best solution? Writing a wrapper over String class?
The freeze method won't work for me. I see that freeze won't allow you to unfreeze the object.
Following is my situation: I have a class that passes a string to a callback. This string happens to be an instance variable of the class and can be potentially large. I don't want the callback to modify it, but still allow the class to modify it at will.
Following is my situation: I have a class that passes a string to a
callback.
Would passing a copy of the string to the callback work?
This string happens to be an instance variable of the class
and can be potentially large. I don't want the callback to modify it,
but still allow the class to modify it at will.
If you're worried about the size of the string, then using String#dup will help. It'll create a new object, with a distinct object_id, but the contents of the string won't be copied, unless the new string (or the original) gets modified. This is called "copy on write", and is described in Seeing double: how Ruby shares string values.
Call #freeze on the String. See: http://ruby-doc.org/core-1.9.3/Object.html#method-i-freeze
I'm trying to call a function, and VS gives me an error (red underline), and i have the option to "generate method stub". What is this?
The generate method stub will generate you a method which looks exactly like you've written it, with the same parameters. Probably are getting this error because you've misspelled the method or because it is in a different namespace.
It means that you're trying to call the function incorrectly; check to make sure you've spelled the method name correctly, and that you're passing it the proper number and types of arguments.
It means you typed a wrong signature, so VS assumes this method doesn't exist. By using the shortcut VS can help you create the method as a stub (i.e. the signature, then you have to fill out the implementation).
ahh I had
method(button.Tag);
and a declaration of
void method(int tag)
so i fixed it with
method(int.Parse(button.Tag.toString()));
i tried that before, but I forgot to put "toString", since I thought it was already an int... stupid little mistake. thx guys