How do I set the content type in Grails? - grails

I'm just trying to setup a very simple controller to return a very simple view (with a very simple layout), however I keep getting errors related to the content type.
class SimpleController {
def index() {
render(view: "simple")
}
}
_simple.gsp:
<html>
<head>
<meta name="layout" content="blank" />
</head>
<body>
Simple test page.
</body>
</html>
blank.gsp:
<!DOCTYPE html>
<html>
<head>
<g:layoutHead />
</head>
<body>
<g:layoutBody />
</body>
</html>
It doesn't get much simpler than that. The controller method is found correctly, the issue seems to be during the rendering. Here's the error I'm getting:
2016-07-05 16:07:14,931 [http-bio-8085-exec-5] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [GET] /testApp/simple/
Stacktrace follows:
Message: null
Line | Method
->> 15 | in com.opensymphony.module.sitemesh.filter.HttpContentType
| 49 | build in com.opensymphony.sitemesh.compatability.PageParser2ContentProcessor
| 89 | getContent . . . . . . . . . . in org.codehaus.groovy.grails.web.sitemesh.GrailsContentBufferingResponse
| 107 | obtainContent in org.codehaus.groovy.grails.web.sitemesh.GrailsLayoutView
Debugging confirms that context.contentType is null. I've found four different way to set the content type, but none of them seem to work.
<%# page contentType="text/html;charset=UTF-8" %>
At the top of the view.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
In the head tag of the view.
contentType: "text/html"
Passed to the render method in the controller.
response.setContentType("text/html")
Called in the controller method.
This is in an existing project, where many other controllers and views work perfectly fine. I need to add a new controller and view, which I originally copied existing working pieces. When that didn't work, I tried to reduce the problem down to be as simple as possible, and ended up with the above code, which is still not working.
Btw, this is with Grails 2.5.1 (stuck with this version).

What you have there should work.
If you're using the default URL mappings you should name your view simple.gsp instead of _simple.gsp
If it's not as simple as that you might try recreating a simple app.

Could be even simpler and incidentally solve your problem.
class SimpleController {
def index() {
}
}
Rename _simple.gsp to index.gsp and needs to be located in views/simple/ directory. If blank.gsp its in views/layouts directory it should work now.

As #jmallen said, the page should be named "simple.gsp".
The underscore at the beginning is something I only know for templates, gsp-files, that could be included in other pages with the following statement:
<g:render template="/layouts/menu" />
than the gsp file for the menu is named
_menu.gsp

Related

Grails: links for text files

In my Grails app I need to serve links to actual text (*.txt) files. If the user clicks the link, they should view the text file as plaintext inside their browser:
MyFiles.groovy controller:
==========================
class MyFiles {
def index() {
render(view: "myfiles")
}
}
myfiles.gsp:
============
<!DOCTYPE html>
<html>
<head>
<!-- Omitted for brevity -->
</head>
<body>
Click me to view a file
</body>
</html>
My questions:
Where should I place myfile01.txt inside the Grails project? Directly inside web-app? Inside a web-app/myfiles dir? Inside WEB-INF?; and
What should the link be in the GSP, such that Grails correctly resolves correctly and displays the file? I don't see anything under g:links that stands out as an obvious choice.
I'd recommend creating another folder where you have images/css.
<a href='<g:resource dir="somedirectory" file=myfiles01.txt" absolute="true" />'>my file</a>

Index ActionMethod in Home Controller would be called twice in ASP.Net MVC

i created an ASP mvc 4 project with basic template and just with one Index view .
when i run the application , Index Method that belongs to Home Controller called twice.
i cant understand why
please help me
Thanks Alot
Index Method:
public ActionResult Index()
{
return View();
}
Index View :
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
</body>
</html>
finally i found the problem
i always test my project on google chrome after changing browser and agin run the project on IE it did not occour the problem again
so my browser which was set in VS2012 , and its setting was the problem

ASP.NET MVC 5 - redirect from view only once

Hy,
I'm trying to redirect users which has deactivated javascript with following code in the main layout (_Layout.cshtml):
<noscript>
<meta http-equiv="refresh" content="0;url=/Error/NoJavascript" />
</noscript>
Rather it works, but it refreshes every time. How can I redirect only once instead of every time?
I've found this code sample, but it doesn't let me compile (error message: can't implicite convert from void to object) I use this code sample in my view (_Layout.cshtml):
#Response.Redirect("/Error/NoJavascript")
Thanks for help :)
Use a simple static HTML file for this special case which has not this meta data:
<meta http-equiv="refresh" content="0;url=#Url.Content("~/Content/NoScript.html")" />
Your returned view from the Error/NoJavascript action method has a reference to the layout page automatically or just set its layout to null.
#{
Layout = null;
}

"Object reference not set to an object" when starting out with Cassette

I'm trying to get started with Cassette via NuGet. I'm having issues with it in my app so I rolled back and tried it in a new empty ASP.NET MVC 3 web application.
However, the problem persists. Following the documentation page "Easy to use", I simply can't get it to work. Here's the exception along with a bit of the stack:
"Object reference not set to an instance of an object."
[NullReferenceException: Object reference not set to an instance of an object.]
Cassette.CassetteApplicationContainer.get_Application() +6
Cassette.Views.Bundles.Reference(String assetPathOrBundlePathOrUrl, String pageLocation) +14
ASP._Page_Views_Shared__Layout_cshtml.Execute() in d:\Dave\Documents\Visual Studio 2010\Projects\CasetteTest\Views\Shared\_Layout.cshtml:2
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
I simply followed the two steps in the documentation and this is what I get. What am I doing wrong?
This is what my _Layout.cshtml file looks like:
#{
Bundles.Reference("Scripts/jquery-1.5.1.min.js");
Bundles.Reference("Scripts/modernizr-1.7.min.js");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>#ViewBag.Title</title>
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
<body>
#RenderBody()
#Bundles.RenderScripts();
</body>
</html>
I figured it out.
I included the Cassette.Views package which does not create a default CassetteConfiguration.cs file that bundles each script and each css file in its own bundle. That's what triggered the NullReferenceException. In order to get it to work, you'll need to add the Cassette.Web package instead. In my defense, the package descriptions in the NuGet gallery are not clear and one is led to believe that the Views package is required for MVC and the other for WebForms.
The next problem was that I referenced the minified '.min.js' scripts which are not picked up by the bundler (it seems).
Cannot reproduce the issue.
4 simple steps allowed me to get a fully working prototype in less than 30 seconds:
Create a new ASP.NET MVC 3 project in Visual Studio
Install-Package Cassette.Web
Index.cshtml:
#using Cassette.Web
#{
Bundles.Reference("~/Scripts/jquery-1.5.1.js");
Bundles.Reference("~/Scripts/jquery-ui-1.8.11.js");
Bundles.Reference("~/Content/site.css");
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Web App</title>
#Bundles.RenderStylesheets()
</head>
<body>
<div>Hello World</div>
#Bundles.RenderScripts()
</body>
</html>
Hit Ctrl+F5 to run the project

Grails UI plugin changing my whole layout of the page

I have installed the GrailsUI plugin from this page http://grails.org/plugin/grails-ui. Installation was successful. I want to use a date picker, in that case I added this code into my view file:
<div class="yui-skin-sam">
<gui:datePicker id='withCalendar' formatString="yyyy/MM/dd" />
</div>
And I added tag into my head tag:
<head>
<meta name="layout" content="main" />
<title><g:message code="User's profile" /></title>
<gui:resources components="['datePicker']"/>
</head>
Ya well its working fine, but the layout of the whole page have changed now, when i added the tag. How do overcome this? Is this is an error ?
And also when ever I select a date from the datepicker, I see this error in my console:
| Error 2012-02-12 12:53:33,592 ["http-bio-8080"-exec-7] ERROR resource.ResourceMeta - Resource not found: /plugins/grails-ui-1.2.3/js/grailsui/SimpleDateFormat-min.js
What does this error means?
Thanks in advance.

Resources