How to access files in the Project Directory using Grails

I needed some templates to display some code to insert users. I put them in

/project-dir/grails-app/resources/templates/quickInstallCode.html 

Then I tried to access them using their relative path ( grails-app/resources/templates/quickInstallCode.html ) and it worked fine.

When we then deployed the application to Tomcat Server using the .war file, the paths began to point to the wrong location.

 ERROR call, Template file /var/lib/tomcat6/grails-app/resources/templates/quickInstallCode.html not found. 

I suggested that Grails, giving good default settings for everyone, would handle this mess for me, but it seems like it isn't.

I also tried this call, and it seems to work fine, but when deploying the BuildSettingsHolder it does not contain build settings, which led to a fatal error.

 BuildSettingsHolder.settings.baseDir.toString() 

http://grails.org/doc/latest/api/grails/util/BuildSettingsHolder.html http://grails.org/doc/latest/api/grails/util/BuildSettings.html

I am very upset that I cannot get this easy task to work, but the reason it is so complicated is that all the files are enclosed in WAR and are not decompressed on the server.

So the questions are:

  • Where in your project would you put files like this?
  • How to get a reliable and stable way to access these files? I just need a stable path to the base directory, without having to hard code something in the configuration ... It can't be that hard.
+4
source share
3 answers

I have 2 solutions for this situation:

  • Save the template in the database in the settings table. This method ensures that nothing can go wrong.

  • You can use the resource folder as suggested by Sachin and Nirmal. About security, I think you can configure SpringSecurity Plugin to protect certain resources so that the user can access only from the site user.

+2
source

Look at the link and try using getResource that spring provides. Its way is more flexible and customizable.

 def filePath = "resources/file.txt" def appHolder=ApplicationHolder.application.parentContext.getResource("classpath:$filePath") 

By the way, Conf is at the root of the class path, you can embed files in src / java or src / groovy.

+2
source

I save my static resources in the web application folder and get them like this ApplicationHolder.application.parentContext.servletContext.getRealPath ("quickInstallCode.html") //quickInstallCode.html should be in the web application folder.

0
source

All Articles