Paul Weber
Paul Weber

Reputation: 6688

How to access files in the Project Directory with Grails

I needed some templates to render some code for users to paste. I put these into

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

Then I tried accessing them using their relative path (grails-app/resources/templates/quickInstallCode.html), and it worked great.

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

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

I assumed, that Grails, giving good defaults for everything would handle this mess for me, but it seems like it does not.

I also tried this call, and it seemed to work great, but when deployed, the BuildSettingsHolder did not contain build Settings, which resulted in 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 pretty frustrated that I cannot get this easy task to work, but the reason that this is so complicated seems to be that all Files are encapsuled in a WAR and not unpacked on the Server.

So the Questions are:

Upvotes: 5

Views: 6389

Answers (3)

Hoàng Long
Hoàng Long

Reputation: 10848

I have 2 solution to propose for this situation:

  1. Save the template in the database, in a setting table. This way guarantees that nothing can go wrong.

  2. You can consider using the resource folder like Sachin & Nirmal has proposed. About security, I think you can configure SpringSecurity Plugin to protect the specific resources, so that it can only be accessed by the site user.

Upvotes: 2

Nirmal
Nirmal

Reputation: 4829

Take a look at this link and try to use the getResource that spring provides. Its way more flexible and configurable.

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

By the way Conf is on the root of the classpath, you can stick the files in src/java or src/groovy.

Upvotes: 2

Sachin Anand
Sachin Anand

Reputation: 468

I keep my static resources in web-app folder and access them like this ApplicationHolder.application.parentContext.servletContext.getRealPath("quickInstallCode.html") // quickInstallCode.html should be in web-app folder.

Upvotes: 0

Related Questions