In grails, is there a way in the i18n file to reference a constant value. I'm trying to do something like this:
constant.sitename=Fancy SiteName
intro.headline.label=Welcome to {constant.sitename}
home.headline=You're at {constant.sitename}
The reason is I don't want to change the sitename in every single string if we decide to change the name, I only want to do it once. Is there a way to accomplish this requirement?
I realize I could also set a constant in the Config.groovy, but then that would require passing in the param on every single message that required it, which I'd rather not have to do this as it would make developers lives worse.
You might override Grails' standard messageSource bean by a custom implementation in resources.groovy. By default the Grails i18n plugin uses PluginAwareResourceBundleMessageSource for this.
So, subclass this and override the necessary code to add property replacement. Don't know by heart what exact methods these are, but that should be easy to figure out with a debugger.
Related
I want to be sure I am not going to cause a performance issue with this. We are writing a simple Money DSL and I was advised to turn on:
ExpandoMetaClass.enableGlobally()
However, I cannot determine yet exactly what this means. Does it only enable metaclass overrides on the inheritance tree for the root object, or does it apply it to every object type in memory?
Are there risks to applying Expando globally to a production instance?
Grails does this already, so whatever cost is involved is already incurred :)
This setting changes the default MetaClass implementation from MetaClassImpl to ExpandoMetaClass. ExpandoMetaClass was written by Graeme for Grails to make it easier to dynamically add methods to the metaclass, e.g.
String.metaClass.bark = { -> println "WOOF!" }
and it was added to Groovy core years ago.
Coming from a Rails background, I don’t really understand what transient means in Grails. I read this, http://www.grails.org/doc/1.3.7/ref/Domain%20Classes/transients.html
So as it says
"...In this case it doesn't make sense to persist this property..."
The guide is referring to String getUpperCaseName() { name.toUpperCase() }
I understand that String name will be picked up when trying to save to the database but why will grails bother with a getter method? It is a method to start with..
The reason why it "bothers" with a method is because of bean naming conventions where properties (in Java) are based on setters and getters. The pattern for identifying a "property" (through reflection) based on these are: getXYZ() and setXYZ() where XYZ is the property name in bean format (name becomes Name and fullName becomes FullName).
The reasoning was the fact these methods are public they can be reflected upon and derive properties from, since actual properties were typically private (for many good reasons). I still recall when this came about. (I'm getting too old)
That's why. Java.
It's not a Grails thing. It's a Java thing. You can read more about the JavaBean specification if you want a deeper understanding of the technologies you are using..
I am a beginner to Struts2. Please tell me why to extend ActionSupport class? (When one doesn't have the requirement of validation or internationalization)
Are there any other benefits provided by extending ActionSupport class?
Convenience and access to basic, common functionality.
It has default implementations of common methods (e.g., execute(), input()), gives access to Action.SUCCESS and other result names, etc.
Note that the I18N functionality goes a bit beyond simple translation, but includes some formatting, allows non-programmers to provide labels/texts, and so on.
There's rarely (ever?) a good reason not to extend it. Even REST plugin actions, e.g., those that handle JSON endpoints, often use validation and I18N support.
IF you don't want to use out of the box features provided by the struts2 you can always avoid using ActionSupport class
This is basically a helper class which provides many out of the box features for you, but at same time Struts2 framework do not ask to use this class, all it want is an entry method for your action class with return type as String and which can throw a general Exception
beside validation or Internationalization this class also provides many other features like Action level Errors etc.
Follow the documentation for detail
ActionSupport
I'm writing a Groovy script (as part of a Grails plugin) and I want to get a list of properties for a GrailsDomainClass that a user of my plugin might define. I can do this using domainClass.properties (where domainClass is a GrailsDomainClass).
However, suppose a user has the grails domain class:
class Example {
String name
static constraints = {
}
def getSomeNonExistingProperty(){
return "Not-a-real-property"
}
}
In this case, domainClass.properties returns a list with both name and someNoneExistingProperty
I understand that this is because of Grails is generating a read-only property on-the-fly for use where someone has a getBlah() method. That's great, but in my script I want to perform some actions with the "real" properties only (or at least non read-only properties).
That is, I would like some way of distinguishing or identifying someNonExistingProperty as a read-only property, or, alternatively, as a property generated by Grails and not entered explicitly as a field in the domainClass by the user of my plugin.
I've looked at the GrailsDomainClassProperty Class and it has a range of methods providing information about the property. However, none of them appear to tell me whether a property is read-only or not, or to allow me to distinguish between a field defined in the domainClass and a field created on-the-fly by Grails as a result of a "getSomeNonExistingProperty()" method.
Am I missing something obvious here? Is there a way of getting a list of just the explicitly user-defined fields (eg name, in the above example)?
I believe transient properties are what you are trying to exclude
I've run into this problem a few times, and instead of trying to work around it I typically just end up renaming my getX() method. It's probably the easiest option.
Edit:
Alternatively, I wonder if you could use reflection to see which methods are defined on the class, and while iterating over your properties see if the property has an explicit getter defined, and omit it. I'm not very familiar with reflection when it comes to Groovy and Grails, especially with the dynamic methods, but it's a possible route of investigation.
I am trying to make a taglib to represent an object (to read and display at the UI). When creating an object (save method in the controller), I see the domain class and association are created by the auto assignment of parameter
def Book = new Book(params)
It also maps complex types (for eg: joda time). I wonder about the naming convention necessary to facilitate this mapping. Out of curiosity, can someone also point where in the grails source code I could see how grails handles this mapping. I'm still learning Spring and probably this would be a good exercise.
Thanks,
Babu.
AFAIK the naming conventions are rather straightforward. If there's a field params.foo and the object you are binding to has a field foo, it will bind the value, assuming the type conversion works properly. If there's a params.bar.id set with an Long value and your object has a complex property of type Bar, it will lookup this instance and inject it.
If you need more control over the binding process, you might want to use bindData.
If you are interested into the details of the binding process, have a look at Java's PropertyEditor as this is what is being used in the background. I wrote a blog post on how to create and register PropertyEditors a while ago, maybe it helps you getting started with that stuff.