Rendering a gsp not working from controller grails - grails

I am facing an issue that i am not able to render a gsp view from a controller.
I did following :
1) create-app demo
2) grails ( open grails cmd)
3) create-controller check.work
package check
class WorkController {
def index() { render(view:'foo') }
}
4) put some content in grails-app/views/work/foo.gsp
5) started grails run-app on 8080
Issue is that if i just render("hello"), it works and prints on browser. But loading gsp file doesn't print any thing and return with 200.
URL used : http://localhost:8080/work/index
Have anyone faced such issue before ?
Grails Version: 3.1.11
| Groovy Version: 2.4.7
| JVM Version: 1.8.0_111

Related

Grails 4.0.2. Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'

I created a new grails app using below code
grails create-app myapp --profile=rest-api
I modified ApplicationController and added namespace to it, It is as below:
class ApplicationController implements PluginManagerAware {
/** The Namespace for the version of the API, see http://docs.grails.org/latest/guide/REST.html#versioningResources */
static namespace = "v1"
GrailsApplication grailsApplication
GrailsPluginManager pluginManager
def index() {
[grailsApplication: grailsApplication, pluginManager: pluginManager]
}
}
There is a index.gson file present under views/application/ Directory.
I run this setup using grails run-app and point my browser to http://localhost:8080
It throws following exeption:
<=======2020-04-08 16:01:31.616 ERROR --- [nio-8080-exec-1] .a.c.c.C.[.[.[.[grailsDispatcherServlet] : Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'] with root cause
However if I modify controller code an use render, everything works fine.
render(view:'index',model: [grailsApplication: grailsApplication, pluginManager: pluginManager])
Output of grails -v
Grails Version: 4.0.2
JVM Version: 1.8.0_171
OS: macOS High Sierra
See Selecting Views For Namespaced Controllers
The correct path for the namespaced view should be:
grails-app/views/v1/application/index.gson

Grails 3 JNDI datasource for Tomcat, Weblogic, Glassfish - Vs Grails 2

I'm experimenting with migrating from Grails 2 to Grails 3.
In Grails 2, I used this as my JNDI name within datasource.groovy file, within the Production-env
jndiName = "${(System.getProperty('catalina.home') && (System.getProperty('java.class.path')).trim().toLowerCase().indexOf('tomcat') > 0 ) ? 'java:comp/env/' : ''}jdbc/myGrails"
I found that worked well for me for Glassfish, Weblogic, and Tomcat.
However, when i try this in Grails 3, within the application.yml:
jndiName: ${(System.getProperty('catalina.home') && (System.getProperty('java.class.path')).trim().toLowerCase().indexOf('tomcat') > 0 ) ? 'java:comp/env/' : ''}jdbc/myGrails
I get this error when running "grails run-app":
| Error Error occurred running Grails CLI: mapping values are not allowed here
in 'reader', line 123, column 169:
... mcat') > 0 ) ? 'java:comp/env/' : ''}jdbc/traxGrails
^
(Use --stacktrace to see the full trace)
If I do use this in Grails 3:
jndiName: java:/comp/env/jdbc/myGrails
Then it works fine when using Grails run-app, and also works fine to deploy the WAR to Tomcat.
Can someone help me in getting my "jndi expression" from Grails 2 to work in Grails 3?
It looks like you're trying to use groovy syntax in a yml file, which does not work. You can create an application.groovy file (which will be merged with the application.yml file) if you want to use groovy syntax.
Also take a look at the many ways spring-boot allows you to inject properties via env variables, properties, yml, etc. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Grails defaultPackage ignored?

When I use create-controller on the command line or in IntelliJ, defaultPackage is ignored. Instead, the controller (or service, etc.) is placed in '#artifact.package.path#':
| Enter a command name to run. Use TAB for completion:
grails> create-controller ThingController
| Created grails-app/controllers/#artifact.package.path#/ThingController.groovy
| Created src/test/groovy/#artifact.package.path#/ThingControllerSpec.groovy
I'm not sure if this is a bug in my code or Grails, perhaps because I moved config from application.yml to application.groovy:
grails{
profile = 'web'
codegen{
defaultPackage = 'com.madeupname.web'
}
spring {
transactionManagement {
proxies = false
}
}
}
Maybe there's a typo I'm blind to? I'm using Grails 3.1.7 on JDK 8.
This will work if you define the property in grails-app/conf/application.yml.
---
grails:
codegen:
defaultPackage: com.madeupname.web

grails 2.3: tests not recognized by test-app

I am playing with the recently released grails 2.3.0. Unfortunately, test-app is not recognizing tests. Here is what I did to produce the problem.
First, make a new app and create a controller:
$ grails create-app firstApp
$ cd firstApp/
$ grails create-controller foo
I got the following files from creating the controller foo:
| Created file grails-app/controllers/firstapp/FooController.groovy
| Created file grails-app/views/foo
| Created file test/unit/firstapp/FooControllerSpec.groovy
Then, I edited the file FooControllerSpec.groovy by adding assert 1 == 2 in the auto-generated method void "test something"(). Here is the full content of FooControllerSpec.groovy after my editing:
package firstapp
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
* See the API for {#link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
*/
#TestFor(FooController)
class FooControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test something"() {
assert 1 == 2
}
}
Then I ran the following command:
$ grails test-app
However, no tests were run. Here is the output from that command:
| Completed 0 unit test, 0 failed in 0m 0s
| Tests PASSED - view reports in /Users/jianbao.tao/projects/grails/firstApp/target/test-reports
My platform is OS X 10.8.5 + grails 2.3.0 + Java 1.6.0_51 + groovy 2.1.6. Can anyone tell me what's going on here, please? Thank you in advance.
Grails 2.3.0 ships with spock test framework by default. So, the test should look like:
void "test something"() {
expect:
1 == 2
}
For details on spockframework, visit docs.

doWithDynamicMethods not being Called

When I create a production war (grails war --nojars) my doWithDynamicMethods is not being called in a production environment. The code works in the dev env (ie grails run-app)
Here is some of my code:
PDFFormsGrailsPlugin.groovy (in PDFForms (plugin) directory) :-
def doWithDynamicMethods = { ctx ->
println "Adding renderPDFForm to controller";
for (controllerClass in application.controllerClasses) {
updateControllers controllerClass.metaClass
}
}
BuildConfig.groovy (in Application using Plugin) :-
grails.plugin.location.'pdff-orms' = "../PDFForms"
We are using Grails 1.3.5 on Windows XP, The Build is called from STS 2.3.3.CI-R5462-B20
Thanks in advance.
Check if the plugin is referenced in your grails.xml.
Read this Nabble thread
And this jira
P.S. Why not you upgrade to grails 1.3.7 at least?!

Resources