5

I need to use some connectors which are actually servlets. How can I do this in Grails and what about the web.xml? How do I configure the url of the servlet?

I actually have a Spring application here and I am trying to convert it into a partial Grails app. I have a connector servlet in the spring-app, which I wish to use here but the mapping is a must to call the servlet in the gsp file. How can I do this? I basically need to know where the xml file is in case of Grails.

cweston
  • 11,297
  • 19
  • 82
  • 107
Rajeev A N
  • 105
  • 3
  • 9
  • This question is too board, u need to narrow down you question to more specific part of what don't you understand, what you tried. – Churk Mar 29 '12 at 11:43
  • 1
    @Churk Thanks churk for the reply i have edited the question to be more specific. – Rajeev A N Mar 29 '12 at 11:49

3 Answers3

8

To get the web.xml file, you can run:

grails install-templates

Then, the file can be found in:

<yourapp>/src/templates/war/web.xml

Edit this as usual to add <servlet> and <servlet-mapping> sections, then put your servlet code in:

<yourapp>src/java/your/package/structure/WhateverServlet.java

and you should be good to go

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thank you, i tried this but due to some reason i get a 404 resource not avilable.I don't know why, And the mapping is absolutely the same as the one in my spring application where the same mapping works perfectly.ALso the folder structure is the same. I am calling this servlet in a javascript from my gsp page.Actually this same page is a html in case of my old spring application. – Rajeev A N Mar 30 '12 at 02:26
  • Make sure to put the url-mapping of your servlet before grails dispatcher servlet – vishesh Oct 25 '17 at 06:28
3

If you are within a grails-plugin, then you have a defined place within your *GrailsPlugin.groovy, where to do such things. E.g. Look at the auto generated closure:

def doWithWebDescriptor = { xml ->
   []
}

In here you can add your custom servlet configurations:

    def servlets = xml.'servlet'
    servlets[servlets.size() - 1] + {
        servlet {
            'servlet-name'('yourName')
            'servlet-class'('yourpackage.YourClass')
        }
    }

    def mappings = xml.'servlet-mapping'
    mappings[mappings.size() - 1] + {
        'servlet-mapping' {
            'servlet-name'('yourName')
            'url-pattern'('/yourPattern/*')
        }
    }
Chris
  • 8,031
  • 10
  • 41
  • 67
-3

good news and bad news, and I myself have asked this question here before. With spring application you can have multiple level of URI such as domain.com/abc/def/efg/abc vs grails has a lot of issue with anything beyond domain.com/controller/view. here is a link to my original question: Grails URL mapping cause error on GSP

The good news is, you don't need to deal with XML mapping, grails does it seemlessly by controllers and views. So you are almost limited to domain.com/YouController/YourView/SomeParamteres... but if thats all you'll need, all u have to do is create grails-app/Controller/SomethingController.groovy and you automatically have domain.com/Something

Community
  • 1
  • 1
Churk
  • 4,556
  • 5
  • 22
  • 37