Grails change web-app static directory - grails

I'm running an application through grails 2.2.4. As it stands, grails handles all requests from the base of the "web-app/" directory. Unfortunately, all the URI references in my web-application are of the type "app/images", "app/styles", "app/scripts", etc. This means that any call to "/images" is hitting "web-app/images", instead of the true "web-app/app/images". Is there a simple way to prefix on an additional "/app/" in front of all calls?
file structure:
/web-app
/app
/images
/styles
/scripts

If you are using Resources plugin (which I think you would be), then have a look at the configuration section.
You would need the below settings in Config.groovy:
grails.resources.work.dir = '/app/'
//if a prefix is required other than 'static'
grails.resources.uri.prefix = '/content/'

Related

Generate URL of resources that are handled by Grails AssetPipeline

I need to access a local JSON file. Since Grails 2.4 implements the AssetPipeline plugin by default, I saved my local JSON file at:
/grails-app/assets/javascript/vendor/me/json/local.json
Now what I need is to generate a URL to this JSON file, to be used as a function parameter on my JavaScript's $.getJSON() . I've tried using:
var URL.local = ""${ raw(asset.assetPath(src: "local.json")) }";
but it generates an invalid link:
console.log(URL.local);
// prints /project/assets/local.json
// instead of /project/assets/vendor/me/json/local.json
I also encountered the same scenario with images that are handled by AssetPipeline1.9.9— that are supposed to be inserted dynamically on the page. How can I generate the URL pointing this resource? I know, I can always provide a static String for the URL, but it seems there would be a more proper solution.
EDIT
I was asked if I could move the local JSON file directly under the assets/javascript root directory instead of placing it under a subdirectory to for an easier solution. I prefer not to, for organization purposes.
Have you tried asset.assetPath(src: "/me/json/local.json")
The assets plugin looks in all of the immediate children of assets/. Your local.json file would need to be placed in /project/assets/foo/ for your current code to pick it up.
Check out the relevant documentation here which contains an example.
The first level deep within the assets folder is simply used for organization purposes and can contain folders of any name you wish. File types also don't need to be in any specific folder. These folders are omitted from the URL mappings and relative path calculations.

How does Tomcat handle overlapping/shadowing names in context paths or URLs?

How does Tomcat 8.0 serve http requests in the following scenario?
Let's say we have deployed the two web applications "ROOT.war" and "Foo.war" on a server with the name "www.host.com".
Furthermore, let's assume that ROOT.war contains a subfolder named "Foo" which contains a file "mypage.html". Additonally, let's assume "Foo.war" also contains a file named "mypage.html". So after extraction of the war-files Tomcat's webapps directory should look like this:
webapps\ROOT\Foo\mypage.html
webapps\Foo\mypage.html
If a user made a request to
http://www.host.com/Foo/mypage.html
in his browser, which file would he be served? The mypage.html from ROOT.war or the one from Foo.war?

Deploy ZF2 site to shared host

When you deploy a Zend Framework website to a shared host, you usually cannot change the DocumentRoot to point at the public/ folder of the website. As a result the URL to the website is now http://www.example.com/public/. This doesn't look very professional, so I'd like to remove it. Up to now I have used ZF1 and Rob Allen kindly provides a method for doing this on his blog http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/ . I have tried to modify this for ZF2. He proposes placing an index.php file in the root with the line:
include 'public/index.php';
After doing this, http://www.example.com opens the index page OK but the CSS links are broken. Rob adds a controller plugin to reset the baseUrl to /public to deal with public facing CSS and image files etc. To do this in ZF2 I found an item from Matthew Weier O' Phinney http://zend-framework-community.634137.n4.nabble.com/Setting-the-base-url-in-ZF2-MVC-td3946284.html where he describes how to set the baseUrl. Based on his code I added this to modules/Application/Module.php
class Module {
public function onBootstrap(MvcEvent $e) {
$config = $e->getApplication()->getServiceManager()->get('config');
$router = $e->getApplication()->getServiceManager()->get('router');
$router->setBaseUrl($config['base_url']);
}
}
The base_url key is set in modules/Application/configs/module.config.php:
'base_url' => '/public'
I was able to dump the router object and confirm that the base_url was being set correctly at this stage. Unfortunately, now http://www.example.com no longer opens the index page and gives a 404 routing error.
Is anyone able to tell me what I am doing wrong or point me in the right direction for running a ZF2 site in a shared hosted environment?
Are you using the skeleton app?
that seems a little over the top, surely it's lot simpler than that.
move everything from public to the root
change index.php
<?php
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
//chdir(dirname(__DIR__));
chdir(__DIR__);
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
Simples.
If you are running an application like this you may want to block direct access to some of the Zend Framework folders using htaccess etc

Grails application root path

Is there a variable where I can find out the root directory of my Grails application?
for example, I have a folder named chart under \%root%\web-app\images\ where I put my charts in. Once I deploy my Grails application on Jetty, I will get FileNotFoundException because the root path becomes /srv/www/vhosts/domain-name/jetty-version/
I would like to know if there is a variable that returns the root path (like /srv/www/vhosts/domain-name/jetty-version/webapps/myapp), and there should be because CSS uses relative path just fine.
solved.
request.getSession().getServletContext().getRealPath("/")
this actually gives me the path to where my application puts the images, css, WEB-INF, etc. folders.
System.properties['base.dir']
I know it is an old question, but this could work if you are not in an http request:
ServletContextHolder.servletContext.getRealPath('/')
If you want to establish this is GSPs try this:
${createLink(uri: '/')}

How to find the physical path of a GSP file in a deployed grails application

I need to find out the physical path of a grails GSP file.
My requirement is that I want to create a new layout file at run-time and use that in the application.
I have been able to achieve this without problem when the application runs on jetty (grails run-app), however, when I deploy the app on Jboss, the path at which the file needs to be created changes.
So, ideally I would like to find out at runtime using some magical utility the path of a particular GSP (lets say main.gsp layout file) and I need to create my new layout in the same directory in which main.gsp reside.
Any pointers?
-Deepak
Here is some groovy code to lookup the layout-folder path in the production environment (eg. when deployed as war on jboss):
import org.codehaus.groovy.grails.commons.ApplicationHolder
...
File layoutFolder = ApplicationHolder.application.parentContext.getResource("WEB-INF/grails-app/views/layouts").file
def absolutePath = layoutFolder.absolutePath
println "Absolute Path to Layout Folder: ${absolutePath}"
File newLayoutFile = new File(layoutFolder, "foo.gsp")
To use the newly created layout, you probably will need to restart the web-application or the container, since the views are chached in production mode.
There's a definitive way...
grailsApplication.parentContext.getResource("WEB-INF/grails-app/views/path/to/my.gsp").file.toString()
Out of controllers (ex. bootstrap)? Just inject..
def grailsApplication
Best regards!
In controllers you can use
grailsAttributes.getApplicationContext().getResource("/relative/path/").getFile().toString()

Resources