Announcement Announcement Module
Collapse
No announcement yet.
Spring / Grails integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring / Grails integration

    Hello all,

    I need to create a Spring MVC Webapplication but I would like to use Grails for front end.

    What is the best way?
    How do I integrate Spring with Grails?
    Shall I use the Grails Controllers or the Spring Controllers ?
    What do I need to do ?

    All sugestions are welcomed

    Thanks a lot for your cooperation

    Kind regards
    Luis Silva

  • #2
    Originally posted by luisfrps View Post
    Hello all,

    I need to create a Spring MVC Webapplication but I would like to use Grails for front end.

    What is the best way?
    How do I integrate Spring with Grails?
    Shall I use the Grails Controllers or the Spring Controllers ?
    What do I need to do ?

    All sugestions are welcomed

    Thanks a lot for your cooperation

    Kind regards
    Luis Silva
    I am not sure what you are getting at with "How do I integrate Spring with Grails?". In Grails we use Spring for a lot of things like dependency injection, data binding, transaction management, etc. Can you clarify what you mean with the question?

    Comment


    • #3
      Groovy issue

      Hello,

      The thing is:

      I decided to create a separate project with Spring and Hibernate just to retrieve information from the DB using Hibernate (instead of letting Grails do it for me under the covers..) but I still want the Grails for the Controller and UI *.gsp.
      *
      In the Grails project I clicked on Properties/Project references/ and selected my "Spring and Hibernate" project so that*my "Grails" project and my "Spring and Hibernate " projects are now connected.
      *
      Inside it compiles with no errors, I can indeed access the java class I defined in my "Spring and Hibernate" project inside a java class of my "Grails" project. If I run the Grails application as a Java application I can acess the java class I defined in my "Spring and Hibernate" with no problem at all. The problem is when I try to run the Grails application.
      Grails console shows an error saying that the java class I defined in the "Spring and Hibermate" project does not exist!
      *
      Do you know what to do in order to solve this dependency?
      *
      Thanks a lot for your cooperation!
      *
      Best regards
      Luis

      Comment


      • #4
        Originally posted by luisfrps View Post
        Hello,

        The thing is:

        I decided to create a separate project with Spring and Hibernate just to retrieve information from the DB using Hibernate (instead of letting Grails do it for me under the covers..) but I still want the Grails for the Controller and UI *.gsp.
        *
        In the Grails project I clicked on Properties/Project references/ and selected my "Spring and Hibernate" project so that*my "Grails" project and my "Spring and Hibernate " projects are now connected.
        *
        Inside it compiles with no errors, I can indeed access the java class I defined in my "Spring and Hibernate" project inside a java class of my "Grails" project. If I run the Grails application as a Java application I can acess the java class I defined in my "Spring and Hibernate" with no problem at all. The problem is when I try to run the Grails application.
        Grails console shows an error saying that the java class I defined in the "Spring and Hibermate" project does not exist!
        *
        Do you know what to do in order to solve this dependency?
        *
        Thanks a lot for your cooperation!
        *
        Best regards
        Luis

        It sounds to me like at least part of your struggle has to do with getting your projects configured in the IDE. I am not sure why you want 2 projects. If you have a bunch of Java or Groovy code that talks to the database (or does just about anything else for that matter) you can put that code in the Grails application under src/groovy or src/java and all of that code is now part of the Grails project.

        It is hard to say without knowing more details about your code but you are probably going to want Grails to help you do things like inject instances of those classes into Grails services and/or controllers by registering corresponding beans in the spring application context, which is very simple to do.

        If you leave this as 2 separate projects then you have to figure out what that means at deployment time. From what you said above, I don't know why you might want 2 separate projects. If for whatever reason you really do need 2 separate projects, you could bundle the non-Grails code up as a library and use that library in the Grails application just like you would use any other library.

        The simplest thing is to just put the code in the Grails application and be done with it. There may be good reasons not to go that route, but they aren't described in this thread so far so I would work with the idea that this is the route to go.

        Is this helping?



        jb

        Comment


        • #5
          Grails controller with Java class

          Hello all,

          Does anyone know the sintaxe for accessing a java class inside a controller in Grails

          I needed to pass the information I have of an object defined in a java package of a Grails project for the controller and vice versa

          Many thanks for you support

          Regards
          Luis Silva

          Comment


          • #6
            Originally posted by luisfrps View Post
            Hello all,

            Does anyone know the sintaxe for accessing a java class inside a controller in Grails

            I needed to pass the information I have of an object defined in a java package of a Grails project for the controller and vice versa

            Many thanks for you support

            Regards
            Luis Silva
            The syntax for accessing a Java class from a Grails controller is the exact same as the syntax for accessing a Groovy class from a Grails controller. There is no difference.

            Code:
            class MyController {
                def someControllerAction() {
            
                    // the Widget class referenced here could be written in Java, 
                    // Groovy, Scala or just about any other JVM language
                    def widget = new com.myapp.Widget()
                    widget.doSomethingUseful()
            
                    // ...
                }
            }

            Comment


            • #7
              Problem accessing DB in Grails controller

              Hello I am new to Grails but I am loving it!

              I am having a problem however accessing a method that I defined in a Java class in my Grails project.

              The method instanciates a class of other project in my workspace that accesses a Derby DB with Hibernate.

              In that external project I had to define in hibernate.cfg.xml the information to access the Derby DB

              <property name="connection.driver_class">org.apache.derby.jd bc.ClientDriver</property>
              <property name="connection.url">jdbc:derby://localhost:1527/HibernateDb;create=true</property>
              <property name="connection.username">user</property>
              <property name="connection.password">password</property>

              In that external Hibernate project the access to the DB works OK but when I use the method inside the Grails controller I get the error:


              Server running. Browse to http://localhost:8080/MyGrailsApp
              | Error 2012-04-12 17:28:06,007 [http-bio-8080-exec-1] ERROR connection.DriverManagerConnectionProvider - JDBC Driver class not found: org.apache.derby.jdbc.ClientDriver
              Message: org.apache.derby.jdbc.ClientDriver
              Line | Method
              ->> 156 | findClass in org.codehaus.groovy.tools.RootLoader
              - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
              | 306 | loadClass in java.lang.ClassLoader
              | 128 | loadClass . . . . . . . . . . . . . . . . . . in org.codehaus.groovy.tools.RootLoader
              | 247 | loadClass in java.lang.ClassLoader
              | 169 | forName . . . . . . . . . . . . . . . . . . . in java.lang.Class
              | 24 | init in com.kuoni.gta.dbacess.SpringHibernateManager
              | 14 | <init> . . . . . . . . . . . . . . . . . . . . in ''
              | 21 | computeValuesFromSpringHibernateManager_Spring in com.kuoni.gta.bl.SpringBusinessManager
              | 9 | retrieveValuesFromSpringBusinessManager . . . in com.kuoni.gta.pl.GrailsManager
              | 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
              | 908 | run . . . . . . . . . . . . . . . . . . . . . in ''
              ^ 662 | run in java.lang.Thread
              | Error 2012-04-12 17:28:06,396 [http-bio-8080-exec-1] ERROR hbm2ddl.SchemaExport - schema export unsuccessful
              Message: JDBC Driver class not found: org.apache.derby.jdbc.ClientDriver
              Line | Method
              ->> 24 | init in com.kuoni.gta.dbacess.SpringHibernateManager
              - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
              | 14 | <init> in ''
              | 21 | computeValuesFromSpringHibernateManager_Spring in com.kuoni.gta.bl.SpringBusinessManager
              | 9 | retrieveValuesFromSpringBusinessManager in com.kuoni.gta.pl.GrailsManager
              | 886 | runTask . . . . . . . . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
              | 908 | run in ''
              ^ 662 | run . . . . . . . . . . . . . . . . . . . . . in java.lang.Thread
              Caused by ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
              ->> 156 | findClass in org.codehaus.groovy.tools.RootLoader
              - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
              | 306 | loadClass in java.lang.ClassLoader
              | 128 | loadClass . . . . . . . . . . . . . . . . . . in org.codehaus.groovy.tools.RootLoader
              | 247 | loadClass in java.lang.ClassLoader
              | 169 | forName . . . . . . . . . . . . . . . . . . . in java.lang.Class
              | 24 | init in com.kuoni.gta.dbacess.SpringHibernateManager
              | 14 | <init> . . . . . . . . . . . . . . . . . . . . in ''
              | 21 | computeValuesFromSpringHibernateManager_Spring in com.kuoni.gta.bl.SpringBusinessManager
              | 9 | retrieveValuesFromSpringBusinessManager . . . in com.kuoni.gta.pl.GrailsManager


              Looks like I have to configure Grails to use the Derby DB as well ?

              Many thanks
              Luis

              Comment


              • #8
                It looks like org.apache.derby.jdbc.ClientDriver isn't on your runtime classpath. Since the code in question works when run from the non Grails project, it must be on the classpath for that project. I assume that you must have configured your Grails app to depend on the other project or you wouldn't be able to create an instance of the class in the non Grails project from the Grails project. If that is the case, verify that the derby jar is being exported from the other project. You could also add the Derby jar as a runtime dependency in the Grails app itself. That would be expressed in BuildConfig.groovy.

                Do you have a plan for how you are going to deploy this once you get out of your IDE? Your Grails app is going to need access to not only Derby, but also your own code that is in this non Grails project.

                Comment


                • #9
                  Hello JBrown,
                  Thank you for your reply,

                  I in fact made the Grails project to depend on the Hibernate project by going to properties/project/references in my IDE and also I have included the jar I did out of the Hibernate project and included in the Grails project. I have also put the derbyclient.jar in the build path of the Grails project and this jar contains the the class that the Grails can not find, so I dont really understand why I can run from the Java Class side the Grails project by running it as a Java application but I can not run if I try to do it as a Grail application, looks like the java side of Grails recognise the external missing class but the Grails at the controller level can not see it. I will check how to force the jar to be exported from the Hibernate project or how to add the jar as a runtime dependency in BuildConfig.groovy

                  As for deployment, I intend to use Maven.

                  Many thanks again

                  Cheers
                  Luis

                  Comment


                  • #10
                    Hello,

                    Does anyone know the syntax for showing error messages when a field is not properly filled on my *.gsp

                    1- The examples I've seen on the internet impose that the constraints for the fields are defined inside the domain class but actually I dont want to use domain classes. I want to have constraints not on domain classes but I want instead to be able to validate the field I have on my gsp.
                    I want an external Spring application to deal with the Business Logic so I would prefer to define my constraints inside the grails controller or by checking what comes in the params and then do something in order to show the error message on the gsp if the gsp fields are not properly filled.

                    2- Also I would need that message to be shown according to the language so I know I have to define the error messages in all the languages in the properties files under i18n but then how do I do it?

                    Thanks a lot for your support

                    Cheers

                    Comment

                    Working...
                    X