最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Grails: Javascript files in views folder - Stack Overflow

programmeradmin4浏览0评论

I'd like to split my views in Grails into 2 files a .gsp file and a .js file so that I get a cleaner Javascript separation from my views. So here's an example:

views/index.gsp
views/index.js
views/home/index.jsp
views/home/index.js

But when I simply add the index.js script reference like this:

<script src="index.js" type="text/javascript"></script>

all I get is a 404.

Does anyone knows how to deal with this?

A great benefit would be to have the ability to use view data inside the index.js file to produce the desired content.

Matthias.

I'd like to split my views in Grails into 2 files a .gsp file and a .js file so that I get a cleaner Javascript separation from my views. So here's an example:

views/index.gsp
views/index.js
views/home/index.jsp
views/home/index.js

But when I simply add the index.js script reference like this:

<script src="index.js" type="text/javascript"></script>

all I get is a 404.

Does anyone knows how to deal with this?

A great benefit would be to have the ability to use view data inside the index.js file to produce the desired content.

Matthias.

Share Improve this question edited Jun 20, 2009 at 20:15 Matthias Hryniszak asked Jun 20, 2009 at 19:58 Matthias HryniszakMatthias Hryniszak 3,1624 gold badges39 silver badges51 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Actually, it should be perfectly possible to serve a JS file (or any other file type) as a GSP from your grails-app/views/ directory. The only thing you have to do, is define a suitable URL mapping for those GSPs, e.g.:

"/javascript/home/index"(view:'/home/index.js')

With this URL mapping, you can put your JS code into grails-app/views/home/index.js.gsp (note the trailing .gsp) and you can use any grails tags in your JS source. To ensure that your JS is delivered with the correct content type, you may want to place

<%@ page contentType="text/javascript"%>

at the beginning of your GSP.

Unfortunately, the createLink tag doesn't support link rewriting to views, but it should be easy to write your own tag to create those links.

Anyways, keep in mind that this won't have a very positive impact on your app's performance. It's usually better to have static JS files (and also serve them as static resources) while passing dynamic stuff as parameters to JS functions for example. This will also keep you from some headaches wrt. caching etc.

The idea is good, but Grails has this directory structure for a reason. The view folder is intended for a certain artifact type (views)..

You could clone your view folder structure under web-inf, but that gives you more work as I guess the idea behind this is to keep related files close together for convenience reasons.

Even though I'm not to excited about storing Javascript together with the view I loved Robert's idea of hooking into the build process by using build events to copy javascript sources into the right directory! If you decide to go down that road you might as well press the sources while you're at it. ShrinkSafe is popular library.

I don't think you are allowed to access js inside views/

if you need to do that ... here is the trick

  1. create your js and rename it with myjs.gsp (use "") iniside _myjs.gsp type you js
... write down you js in here ...
  1. inside you gsp (for example: index.gsp, view.gsp, etc) type this tag to upload you js

Update 2:

Grails offer the possibility of hooking into the build lifecycle using custom events.

An event handler can be written which synchronises all JavaScript files under grails-app/views with the target folder of web-app/js.

Place the custom code in $PROJECT/scripts/Events.groovy. The PackagingEnd is a good target for the invocation, since it happens right after web.xml is generated.

eventPackagingEnd = {  ->
     // for each js file under grails-app/views move to web-app/js
}

Update

If you'd like the JavaScript files simply 'meshed' together, you can do that using symlinks, e.g.:

grails-app/views/view1/index.js -> webapp/js/view1/index.js

As far as I know, there is no way of forcing grails to directly serve content which is outside of web-app.

Alternatively, you can inline your JavaScript, but that can have performance implications.


JavaScript files belong under web-app/js.

Then you can reference them using <g:javascript src="index.js" />.

发布评论

评论列表(0)

  1. 暂无评论