Grails layouts not applied on a 404 UrlMapping - grails

In a Grails 1.3.1 app, I want 404 errors to cause the render the main index action of the "list" controller. This controller uses a conventional layout file in the layouts directory (views/layouts/list.gsp), which then uses the included snippet for that action (views/list/index.gsp).
When this action is accessed normally (http://localhost/list/index), then both the layout file and the snippet are applied, and it looks correct. However, when accessed via a 404 rule in the UrlMapping class -- "404"(controller: "list", action: "index") -- the layout file isn't used, and only the snippet is displayed.
Does anyone know why this happens, and if there's any way to get the conventional layout to display for a 404 (or other) error mapping?

I know a while back this was a bug in the version of SiteMesh Grails was using. There is a work around where you can wrap your error pages in:
<g:applyLayout name="main">
</g:applyLayout>
Instead of using the usual:
<meta name="layout" content="main" />

Another thing to look for is the sitemesh.xml configuration file. Sitemesh is turned on/off depending on the content-type of the response, and this file declares the content-types values that site-mesh will process. In my case, an entry for text/html;charset=UTF-8 was not enough for responses with type text/html to be processed.

Are you sure the layout isn't applied? I'm using Grails 1.3.2 and I thought the layout wasn't applied, however it actually was the lack of a model and security tags in the layout after a 404.
If your layout content is derived from such things being available, try a "Hello world" first to see if it shows up.

Related

load jquery library in grails 2 is failing?

I was following the manual
https://grails.github.io/grails2-doc/2.2.0/guide/single.html#ajax
there it says to put the following tag in head section
<g:javascript library="jquery" />
I created a sample application and a test controller called HomeController. I created index page and put the above tag in the head. When i load the page it throws the following error.
I am finding the error difficult to comprehend. I appreciate any help. Thanks!
If your index page is not using any layout then make sure you have <r:layoutResources/> at the end of your head and body.
And if using layout, then make sure you have <r:layoutResources/> in your layout gsp (at the end of head and body).

Difference Between Script Rendering Code in Razor

When I create an MVC project that uses Razor, the following lines are generated:
#Scripts.Render("~/bundles/jquery")
#RenderSection("scripts", false)
What is the difference between these lines ? Is it the case that #Scripts.Render generates a <scripts> section and then #RenderSection("scripts", false) renders it ?
The first one renders out a bundle, which is a group of related Javascript files. For instance, you might want to bundle jQuery and jQuery UI together. Bundles also get the benefit of bundling and minification when a solution is compiled in release mode. ref: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
#RenderSection indicates that a page that uses the layout in question can inject markup in a particular spot in the layout. Sections are in effect a placeholder (and work much like the ContentPlaceHolder server control from web forms if you are familiar with that). That reference is probably right before the closing body tag, where it is believed by some to be the best spot to put scripts. You could have a section called scripts, or head, or footer, it is completely arbitrary and sections don't necessarily have anything to do with scripts at all. ref: http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor
These are completely different things.
The #Scripts.Render("~/bundles/jquery") is rendering a group of scripts for you, creating all the <script> tags. in the BundleConfig.cs file you are able to create bundles of scripts and css files.
One of its advantages allow you to group multiple files that are commonly used together. Instead of including each of these files explicitly you can include all of them using explicit Scripts.Render(groupName).
You can read more about Bundling and Minification here.
The #RenderSection("scripts", false) is about rendering a section in the view.
A section allow you to specify a region of content within a layout. It expects one parameter which is the name of the section. If you don’t provide that, an exception will be thrown.
Here is a good article explaining about Layouts, RenderBody, RenderSection and RenderPage in ASP.NET MVC.
The first parameter to the “RenderSection()” helper method specifies
the name of the section we want to render at that location in the
layout template. The second parameter is optional, and allows us to
define whether the section we are rendering is required or not. If a
section is “required”, then Razor will throw an error at runtime if
that section is not implemented within a view template that is based
on the layout file (which can make it easier to track down content
errors). If a section is not required, then its presence within a
view template is optional, and the above RenderSection() code will
render nothing at runtime if it isn’t defined.

Render a page in grails without html and body tag

I want to render a page in grails without a <html>, <head> and <body> tag, I just want DIV and tables.
I want to integrate this page in my Facebook page and the requirement of Facebook page is that the page should not contain <html>, <head> or <body> tags.
You can use a gsp-template (GSP file name start with '_'). Than you can call you controller an use the render method.
class MyDomainController{
def myAction = {
...
render(template:'myTemplate', model: ...)
}
}
Define a layout that only contains the <g:layoutBody /> tag.
I recommend reviewing Grails Web Layer. Note that if you aren't using layouts then you can simply omit the head and body tags. SiteMesh (and Grails) doesn't care if they are there or not. Another poster recommended a template. If you are using layouts with SiteMesh then it might be easier to simply render a template then write a overriding layout.
Grails do not care, if there is a html and head tag in your gsp, and if you choose to make a gsp without a template, that's fine too. If you only make a page fragment, grails will render this happily.

grails app root context

I have a test grails app setup with a context of "/testapp". When I add a link in my gsp that references / it does not go to the root of my grails.app.context, but to the root of my grails.serverURL property.
For example given a link with href "/css/main.css"
I would expect that this link would actually look in localhost:8080/testapp/css/main.css instead of localhost:8080/css/main.css
Is there a way that I can get references to / to start at my grails.app.context vs the grails.serverURL?
use the request contextPath value on the page
${request.contextPath}
and then prepend the additional host information if necessary to construct the complete url
the question is how do you add your links into your gsps?
We do things like
<link rel="stylesheet" href="${resource(dir: 'css', file: 'stylesheet1.css')}"/>
and
<g:javascript library="prototype"/>
by using the g:javascript and resource tags and methods, you tell grails to set the path for you...
I suspect you are just putting standard tags in...
goto
http://grails.org/doc/latest/
and, under tags in the left hand nav, look for resource and/or javascript to get an idea (its difficult to link directly in to the docs...:()
I had a similar issue to OP - how to have grails form links that start at the context root and NOT server root?
You can do so using the "uri" attribute for g:link and g:createLink tags. For example:
<g:link uri="/login">login</g:link>
will prefix any context if applicable, and produce the following
login if your app is at the http://server/
login if your app is at http://server/testapp/
Not sure why it's an undocumented attribute in the reference docs, but I found it in the Javadocs - ApplicationTagLib
You should probably be using the resource tag into your grails CSS directory, like mentioned above. However, you can also use the resource method to find the root context of you web application using the same tag:
${resource(uri:'/')}
then just use that string wherever.
And when it comes to elements like stylesheets I'd recommend creating a simple tag that'll do the trick, something along those lines:
class StylesTagLib {
static namespace = "g"
def stylesheet = { args, body ->
out << """<link rel="stylesheet" href="${resource(dir: 'css', file: args.href)}"/>"""
}
}
and later on in your code use it like this:
<g:stylesheet href="main.css"/>
Obviously you can fiddle with the conventions (should I use a predefined folder? should I add the .css extension automatically? stuff like that) but the general idea is to hide the ugliness behind a nicely defined tag.

Avoid "The class or CssClass value is not defined" Warnings in ASP.NET MVC ASCX Partial Views (UserControls)

I wondered how I can stop those annoying compiler warnings "The class or CssClass value is not defined" coming from my ASP.NET MVC partial views/ASCX user controls. The CSS is correctly defined, but the reference to the CSS file is obviously on the master page only. My partial views contain lots of CSS class references on div's etc. and so I get massive amounts of warnings which I shouldn't see.
How can I solve this?
Thank you !
Include this in your partial view:
<% if (false) { %>
<link rel="stylesheet" type="text/css" ...
<% } %>
This will make intellisense happy, and excludes the stylesheet when the page is rendered, so that it is not included twice.
One way is to turn HTML Syntax Checking off (Tools->Options->Text editor->HTML->Show Errors->In CSS).
I use the Firefox Tidy plug in, which gives better advice, IMHO.
This is not a flaw in ASP.Net MVC, and I don't think it's going to be it's going to be fixed in the next version. This is a 'limitation' (notice how i avoid the word flaw) in asp.net (not just mvc) that prevents it from accessing header information that's included in the master page. So you don't have access to javascript/css in the content pages/usercontrols.
The code provided by Robert Harvey is a hack solution that we've been using to overcome this.
It works by using the enclosing the include file in an if block that's always false. So the compiler sees the css file but the runtime doesn't.

Resources