What is the difference between a layout a view and a template in grails




















Description A multi-purpose method for rendering responses to the client which is best illustrated with a few examples! File, or inputStream you wish to send with the response fileName optional - For specifying an attachment file name while rendering a file.

Quick Reference hide. Command Line Usage. Constraints Usage. I described what I think is the real problem in a separate answer. I hope that helps. Add a comment. Active Oldest Votes. Thanks that did it. And I changed it to template to accommodate the underscore.

I would also move it out of the layouts directory for the reason I cited in another comment. I am glad that you got it to work. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. For example you could get a query string such as? In this case dealing with one parameter and dealing with many has different semantics since Groovy's iteration mechanics for String iterate over each character.

To avoid this problem the params object provides a list method that always returns a list: for name in params. If a controller declares a method that accepts a single argument and the argument type is java. Exception or some subclass of java. Exception , that method will be invoked any time an action in that controller throws an exception of that type.

The name is not what makes the method an exception handler, the Exception argument type is the important part. The exception handler methods can do anything that a controller action can do including invoking render , redirect , returning a model, etc. One way to share exception handler methods across multiple controllers is to use inheritance.

Exception handler methods are inherited into subclasses so an application could define the exception handlers in an abstract class that multiple controllers extend from. Another way to share exception handler methods across multiple controllers is to use a trait, as shown below Specifically, exception handler methods which are runtime metaprogrammed onto a controller class are not supported. It is designed to be familiar for users of technologies such as ASP and JSP, but to be far more flexible and intuitive.

Although it is possible to have Groovy logic embedded in your GSP and doing this will be covered in this document, the practice is strongly discouraged. Mixing mark-up and code is a bad thing and most GSP pages contain no code and needn't do so. A GSP typically has a "model" which is a set of variables that are used for view rendering. The model is passed to the GSP view from a controller. The import directive lets you import classes into the page.

Embedding data received from user input has the risk of making your application vulnerable to an Cross Site Scripting XSS attack. The section on Tag Libraries covers how to add your own custom tag libraries. All built-in GSP tags start with the prefix g:. Unlike JSP, you don't specify any tag library imports. If a tag starts with g: it is automatically assumed to be a GSP tag. Date instance. GPath is an XPath-like language in Groovy.

The books variable is a collection of Book instances. Since each Book has a title , you can obtain a list of Book titles using the expression books. Groovy will auto-magically iterate the collection, obtain each title, and return a new list! The link tag lets you specify controller and action name pairing and it will automatically work out the link based on the URL Mappings , even if you change them!

Beyond that all of the usual HTML attributes apply. Multiple Submit Buttons The age old problem of dealing with multiple submit buttons is also handled elegantly with Grails using the actionSubmit tag. Tags as method calls from GSPs Tags return their results as a String-like object a StreamCharBuffer which has all of the same methods as String instead of writing directly to the response when called as methods. These are useful for partitioning your views into maintainable chunks, and combined with Layouts provide a highly re-usable mechanism for structured views.

Template Basics Grails uses the convention of placing an underscore before the name of a view to identify it as a template. However, you may want to share templates across your application. Triggering Layouts There are a few ways to trigger a layout. Layout by Convention Another way to associate layouts is to use "layout by convention". If you have both the above mentioned layouts in place the layout specific to the action will take precedence when the list action is executed.

Inline Layouts Grails' also supports Sitemesh's concept of inline layouts with the applyLayout tag. This can be used to apply a layout to a template, URL or arbitrary section of content. This lets you even further modularize your view structure by "decorating" your template includes. This plugin is installed by default in new Grails applications. The basic way to include a link to a static asset in your application is to use the resource tag. This simple approach creates a URI pointing to the file.

However modern applications with dependencies on multiple JavaScript and CSS libraries and frameworks as well as dependencies on multiple Grails plugins require something more powerful. The issues that the Asset-Pipeline plugin tackles are: Reduced Dependence - The plugin has compression, minification, and cache-digests built in. Easy Debugging - Makes for easy debugging by keeping files separate in development mode. Asset Bundling using require directives. Web application performance tuning is difficult.

The need for a standard way to expose static assets in plugins and applications. The asset-pipeline allows you to define your javascript or css requirements right at the top of the file and they get compiled on War creation. Take a look at the documentation for the asset-pipeline to get started. By default the plugin sets the disposition to be "head", so they load early in the page.

Rendering the links to resources with r:layoutResources When you have declared the resource modules that your GSP page requires, the framework needs to render the links to those resources at the correct time.

The Resources framework has the concept of a "disposition" for every resource. This is an indication of where in the page the resource should be included. The default disposition applied depends on the type of resource. Page load times are improved when JavaScript is loaded after the page content, so the default for JavaScript files is "defer", which means it is rendered when the second r:layoutResources is invoked.

The only limitation is that you must call r:require before the r:layoutResources that should render it. Adding page-specific JavaScript code with r:script Grails has the javascript tag which is adapted to defer to Resources plugin if installed, but it is recommended that you call r:script directly when you need to include fragments of JavaScript code. Note that such r:script code fragments always load after any modules that you have used, to ensure that any required libraries have loaded.

With this mechanism you can specify the width, height and any other attributes in the resource declaration in the module, and they will be pulled in as necessary. The Grails img tag is Resources-aware and will delegate to r:img if found. However it is recommended that you use r:img directly if using the Resources plugin. Alongside the regular Grails resource tag attributes, this also supports the "uri" attribute for increased brevity.

See r:resource documentation for full details. Grails' own g:resource tag delegates to this implementation if found, but if your code requires the Resources plugin, you should use r:resource directly. This can go either in your Config. Note that you do not need to declare all your static resources, especially images.

However you must to establish dependencies or other resources-specific attributes. Any resource that is not declared is called "ad-hoc" and will still be processed using defaults for that resource type. The resources in these modules will be automatically bundled out of the box according to the module name, resulting in fewer files. You can override this with bundle:'someOtherName' on each resource, or call defaultBundle on the module see resources plugin documentation.

It declares dependencies between them using dependsOn , which controls the load order of the resources. You'll also notice the disposition:'head' on the core. The CSS file for print styling adds custom attributes using the attrs map option, and these are passed through to the r:external tag when the engine renders the link to the resource, so you can customize the HTML attributes of the generated link. There is no limit to the number of modules or xxxResources. To define modules like this in your application's Config.

For full details of the resource DSL please see the resources plugin documentation. For example, you may wish to bundle jQuery and some other libraries all together in one file. There is a load-time and caching trade-off here, but often it is the case that you'd like to override some of these settings.

Services Usage. Servlet API request. Tag Libraries Usage. Tags actionSubmit.



0コメント

  • 1000 / 1000