We are using Grails 2.4.3 , In our Command
#Validateable
class FreeTextSearchCommand {
String freeText="*"
int pageSize=1
int pageIndex=1
}
And in Controller
def freeTextSearch(FreeTextSearchCommand freeTextSearchCommand){}
So if value comes from Client site is null then I want my default value set , but it does not work
So if value comes from Client site is null then I want my default
value set , but it does not work
If you bind null to a property then the result should be null. That is what the data binder does and it is what the binder is supposed to do. If you want to impose some custom rules you can do that a number of ways. One is by using the BindUsing annotation.
#Validateable
class FreeTextSearchCommand {
#BindUsing({obj, source ->
// bind '*' if there is no corresponding request parameter value...
source['freetext'] ?: '*'
})
String freeText
// etc...
int pageSize=1
int pageIndex=1
}
See http://grails.github.io/grails-doc/2.4.3/guide/theWebLayer.html#dataBinding for more details.
I hope that helps.
Related
the key for the messages.properties lookup are provided by my Java Class, as you can see here (where i is a variable in a th:each loop):
th:text="#{${i.nameKey}}"
public String getNameKey() {
// getKeyPrefix() generates a unique name from the class attributes
return getKeyPrefix() + ".name";
}
This works fine.
Now I want to check if the value in the messages.properties file is null or empty for the given key:
th:if="${#messages.msg(i.nameKey) != null AND NOT #string.isEmpty(#messages.msg(i.nameKey))}"
But i can't pass the messages object into the isEmpty method.
Any idea?
Thanks!
#messages.msg does not return null if the key is missing. From the docs:
If a message is not found, a default message (like '??msgKey??') is returned.
You should use #messages.msgOrNull instead. As for the second part of the question, there is no reason you can't combine #utility objects. That being said, it looks like you have a typo: the utility object #string does not exist, but #strings does.
th:if="${#messages.msgOrNull(i.nameKey) != null AND NOT #strings.isEmpty(#messages.msgOrNull(i.nameKey))}"
void main() {
foo(bar: 1);
}
void foo({#required int bar}) {} // Error
Error:
The parameter 'bar' can't have a value of 'null' because of its type, and no non-null default value is provided.
I am annotating bar with #required and it is also non-nullable. That means I'll always have to provide bar a non-null value. So, why does the compiler ask me to provide a default value?
#required is the old annotation tag coming from the meta package and was introduced as a way to give warnings from the analyzer. With NNBD this has been changed to a keyword called required. You can read more about this keyword in the following link:
https://dart.dev/null-safety/understanding-null-safety#required-named-parameters
Is there a way to get the string value out of a KeySym value?
For example, out of keyPrintable("a").
If you know the KeySym value is a keyPrintable, you can just get it using the key property. For instance
KeySym kv = ... // something that yields a KeySym
str s = kv.key;
If you don't know it's a keyPrintable you can either check to see if it was built using that constructor, or use pattern matching. So, either
if (kv is keyPrintable) {
// code that uses kv.key to get back the value
}
or
if (keyPrintable(str s) := kv) {
// code that can now use s, which is the key
}
You can also ask if kv has that field, and then use it:
if (kv has key) {
// code that uses kv.key
}
Once you introduce a field name in a constructor, and it has a specific type, you know that same field name has that same type in any additional constructors for the same datatype. So, once we know field key is type str, field key has to be str in any value of type KeySym. That is why it's fine to see if kv has field key and then treat it as a str, nobody could come along later and add a new constructor for KeySym where key has a different type.
I'm using Grails 2.3.7 and have a controller method which binds the request to a command class. One of the fields in the command class is a String array, as it expects multiple params with the same name to be specified - e.g. ?foo=1&foo=2&foo=3&bar=0.
This works fine most of the time. The one case where it fails is if the param value contains a comma - e.g. ?foo=val1,val2&foo=val3,val4. In this case I'm getting an array with 4 values: ["val1","val2","val3","val4"], not the intended output of ["val1,val2","val1,val2"]. URL escaping/encoding the comma does not help and in my cases the param value is surrounded by quote characters as well (foo=%22a+string+with+commas,+etc%22).
I had a similar problem with Spring 3.x which I was able to solve: How to prevent parameter binding from interpreting commas in Spring 3.0.5?. I've attempted one of the answers by adding the following method to my controller:
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String[].class, new StringArrayPropertyEditor(null));
}
However this didn't work.
I also tried specifying a custom conversion service, based on the comment in https://jira.grails.org/browse/GRAILS-8997.
Config/spring/resources.groovy:
beans = {
conversionService (org.springframework.context.support.ConversionServiceFactoryBean) {
converters = [new CustomStringToArrayConverter()]
}
}
and
import org.springframework.core.convert.converter.Converter
import org.springframework.util.StringUtils
class CustomStringToArrayConverter implements Converter<String, String[]> {
#Override
public String[] convert(String source) {
return StringUtils.delimitedListToStringArray(source, ";");
}
}
but I couldn't get this to work either.
For now, I've come up with a work around. My Controller method has an extra line to set the troublesome field explicitly with:
commandObj.foo = params.list('foo') as String[]
I'm still open to suggestions on how to configure grails to not split on a comma...
With the code below I am having an issue where not all the columns are return data in the data.results array. For example if col4 is null in the database for row 1 then data.results[0] does not contain an element for col4, but row 2 has a value then data.results[1] will contain the value for col4. I would like each return item in the array to contain all items with the database value or null. If null can't be returned then an empty string would do.
var query = new breeze.EntityQuery()
.from('mytable')
.where('col1', 'substringof', '2')
.select('col1,col2,col3,col4')
.orderBy('col1')
.take(200);
return _manager
.executeQuery(query)
.then(function (data) {
return data.results;
})
.fail(queryFailed);
}
By default breeze does not serialize null values in its JSON results. This was a deliberate choice to reduce the breeze payload over the wire. And.. this is not typically an issue with queries that return "entities". i.e. data for which breeze metadata exists. Because the properties are already defined on such entities.
But if you are returning an anonymous result as you are, then this can be a problem. You can obviously work around it because you know the properties that you are requesting and can update them after the query if they are not in the result.
But you can also change breeze's default configuration to accommodate this via the "BreezeConfig" class.
BreezeConfig enables customization of components supporting Breeze-related operations in the Web API. BreezeConfig defines default behaviors; you can substitute your own behaviors by deriving from it and overriding its virtual methods. Breeze.NET will discover your subclass among the assemblies referenced by your project and use it instead of BreezeConfig.
To use BreezeConfig to configure the Json.Net serializer with specific settings. You can replace those settings by writing a subclass of BreezeConfig that overrides the 'CreateJsonSerializerSettings' method as shown in this example:
public class CustomBreezeConfig : Breeze.WebApi.BreezeConfig {
protected override JsonSerializerSettings CreateJsonSerializerSettings() {
var baseSettings = base.CreateJsonSerializerSettings();
baseSettings.NullValueHandling = NullValueHandling.Include;
return baseSettings;
}
Hope this helps.