views:

532

answers:

2

I'm trying to add a mime-mapping element to the web.xml.

My current best stab is:

def doWithWebDescriptor = { xml ->
    xml + {
        'mime-mapping' {
            'extension'("htc")
            'mime-type'("text/x-component")
        }
    }
}

I know the code is being run as the above actually outputs an invalid web.xml. The following seems to be more logical but it doesn't actually have any effect:

def doWithWebDescriptor = { xml ->
    xml.'mime-mapping' + {
            'extension'("htc")
            'mime-type'("text/x-component")
        }
}

edit: I'm using grails 1.0.3

A: 

Hmm, the only thing I could find referred to a config property that must be set in order to properly access mime-types in requests:

grails.mime.file.extensions = true

Try making sure this is set to true, and then try it again with the code that works?

Bill James
not quite what I'm after I'm afraid, I tried this first, it doesn't seem to work for static files under web-app
Gareth Davis
Yup, sorry, but it was the only docs I could find that seemed somewhat relevant. Good luck.
Bill James
+1  A: 

Try

def doWithWebDescriptor = { xml ->
    xml << {
        'mime-mapping' {
            'extension'("htc")
            'mime-type'("text/x-component")
        }
    }
}

(note the leftShift instead of the plus).

Alternatively, if you want to ensure that your new element is inserted at a specific position within the XML, you can get the child element, after which you want your element to be inserted, and add yours with the plus operator. For example, I use the following code to add a new servlet-mapping:

def servletMappings = xml.'servlet-mapping'
servletMappings[servletMappings.size() - 1] + {
    'servlet-mapping' {
        'servlet-name'("myServlet")
        'url-pattern'("/myURL")
    }
}

If you want to dig further into this, have a look at groovy.util.slurpersupport.NodeChild (the xml argument is of that type) and its superclass groovy.util.slurpersupport.GPathResult.

Good luck!

Daniel Rinser
great answer...much appreciated
Gareth Davis