I need to have access to my properties file from GSP file to get one of it's values.
I tried to find example over the web, but I couldn't.
This is a file which I created and not Grails.
Thanks!
From grails controller and gsp file, you can access your yml file properties using GrailsApplication object:
${grailsApplication.config.get('YOUR_PROPERTY_NAME)}
you can access aaplicationContext from the GSP ... so you can access any configuration property.
Just look at this entry from Mr. Haki:
http://mrhaki.blogspot.com.es/2011/11/grails-goodness-get-grailsapplication.html
If the properties file is under grails-app/i18n/ (it can be your own properties file, doesn't have to be one of the default ones) then you can access property values from the property file in a GSP using the message tag. See http://grails.org/doc/latest/ref/Tags/message.html.
If you are reading simple properties it sounds like what you want is externalized configuration
To use it in a GSP (if you really need to, but not recommended from a GSP):
${Holders.config.myPropertyKey}
Related
In a gsp file there is line like below
<g:each in="${tools}" var="tool" status="counter">
and when i debug it shows value like below
It's using Grails framework. May I know how can I find where is this ${tools} declared and being called from to this GSP?
Grails uses convention over configuration. So in order to trace back where tool is being set, you have to note the name of the gsp file and the directory in which it is placed.
Ideally if the gsp name is bar.gsp and is placed under foo directory under grails-app/views then the convention is that there could be a FooController.groovy under grails-app/controllers which would have an action called bar(). Most likely the model is set with the variable tools inside the baraction.
This exercise would be a good starting point.
What #dmahapatro suggested is the best approach but if you are still unable to find that tools variable and simply do a project level search in the IDE you are using and find the word tools.
Here are some filters you can apply to fasten your search:
First, search only in the directory grails-app/controllers
Then search in the directory grails-app/directives
Try to search following words: tools, tools:, tools : because it will be rendered as a map.
I'd like to store properties in a database tables and have defaults for those properties set in Config.groovy. For example, I want to put a default email address into Config.groovy:
app.send.report.to = 'me#example.com'
and then be able to override this in a database table (key, value columns...).
Is there a plugin (or functionality inside grails) to do this?
There is Dynamic Config Plugin.
It stores config property in ConfigProperty domain and merges properties from Config.groovy and from database using:
grailsApplication.config.merge(configObject)
You may want to look at the plugin source code. If plugin does not work for you, you can implement something similar to this.
This approach is useful when you have UI for editing config properties.
Grails does not have functionality that I'm aware of to override configuration values from a database, but it shouldn't be that difficult to do. In your Config.groovy you could put the defaults, and then as part of your bootstrap process, you could generate a temporary config file that has the values from the database (a simple query and iteration over the results could be used to generate that temp file). Include that temp file as one of your grails config locations, and it will override any values that are in the Config.groovy
If your goal is to have a shared configuration file that is used by multiple grails apps, you might also look into using something like Zookeeper to manage the shared configuration, but that may be a bit overkill for a single config file.
Not quite what you're asking for, but depending on what you want to achieve the External Configuration Reload plugin might be of use. It helps you to override default properties (in runtime), but not by using the DB.
I have a java annotized domain object that I want to use in grails, this works fine, however all the generated templates use Id as the primary key. I tried to create a XxxConstraints.groovy file in the same package as my domain object, and added
mapping = {
id type:'assigned', name:<name of java field>, type: string
}
but that does not seem to work.
The error I get when trying to render the gsp is "class XYZ does not contain field id"
By the way I am using grails 2.1.0.
Thanks for the help in advance.
If you're talking about scaffolding views, they just have id field hardcoded, like: <g:hiddenField name="id" value="\${${propertyName}?.id}" /> (sample from edit.gsp).
Just modify your GSPs by hand to use your key field.
If it's more than one class and you don't want to generate all the views, you can install the templates into your project sources with grails install-templates and modify those to use your PK field. Maybe Groovy wrappers for Java classes will have ident() method, though not sure.
After looking at the doc, I'm in doubt if mapping{} DSL will work at all. Looks like only constraints section will work, and I believe you only need #Id annotation.
I have a custom external XJB file that has the schema name within it as follows :
jxb:bindings schemaLocation="completeCheck.xsd" node="/xs:schema"
Just wondering, is there a way to substitute the schema name at runtime within the XJB file using ANT(using ANT XJC) OR have a xjb binding file written such that I do not have to hardcode the schema name in it. Basically, I am trying to see if I can use a single xjb file for multiple XSDs.
Currently, I have the same xjb file all over the place with a different hardcoded schema name referred all over the place in build.xml. Any pointers are highly appreciated. I am using JAXB 2.x
Make a template from your xjb file by replacing the value of your schemaLocation attribute with some sort of marker (e.g. !!!) and save it using some other name/extension. Define a macro with an attribute such as schema-name that will use ant's replace to change the marker in the template file with your schema name and save that as the binding file to be subsequently used by xjc.
Normally, in the latest versions of XJC, you can use schema component designator. More info here :
https://jaxb.java.net/nonav/2.2.1/docs/vendorCustomizations.html#scd and https://jaxb.java.net/guide/Using_SCD_for_customizations.html
This GSP:
<g:link controller="book" action="show" id="5">Example</g:link>
results in this HTML:
Example
This is relative to the HTTP host. If your site is running at http://localhost:8080 that's fine.
During development, an app will more commonly be running at http://localhost:8080/appName.
In such cases, the above link will not work - it will result in an absolute URL of http://localhost:8080/book/show/5 instead of the required http://localhost:8080/appName/book/show/5
What changes are required of the above GSP for the app name to be present in the resulting anchor's href?
The configuration setting grails.app.context should be equal to the context where you want your application deployed. If it's not set, as in the default configuration, it defaults to your application name, like http://localhost:8080/appName. If you want to deploy your app in the root context (e.g. http://locahost:8080/), add this to your Config.groovy:
grails.app.context = "/"
If the context is properly set, the URLs generated by g:link tags will include the context before the controller name.
I found that the meta tag is very useful for getting information in GSP files.
For example, if you want your application name, you can get it like this:
<g:meta name="app.name"/>
You can get any property in your application.properties file like that.
And if you, like me, need to concatenate it to another value, here is my example. Remember that any tag can be used as a method without the g: namespace. For example:
<g:set var="help" value="http://localhost:8080/${meta(name:"app.name")}/help" />
Grails documentation about this is a little poor, but it is here.
For me the single best reason to use <g:link> is that it adds the context if there is one, or omits it if you're running at http://localhost:8080 or in prod at http://mycoolsite.com - it's trivial to just concatenate the parts together yourself otherwise.
The same goes for using g:resource with css, javascript, etc. - it lets you have one GSP that works regardless of what the context is (e.g. 'appName'), since it's resolved at runtime.
I think that is what grails.serverURL is for. You defined this config variable in the Config.groovy, check the configuration documentation for Grails for more details.
Hope this helps!
createLink tag includes your appname / context parameter automatically in the link.
Here is reference doc for it.