Skip to content

Commit c873c83

Browse files
author
graeme
committed
fix for GRAILS-883
git-svn-id: https://svn.codehaus.org/grails/trunk@6267 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent b1de1c6 commit c873c83

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/groovy/org/codehaus/groovy/grails/plugins/web/ServletsGrailsPlugin.groovy

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import javax.servlet.http.HttpServletRequest
2020
import javax.servlet.http.HttpServletResponse
2121
import javax.servlet.http.HttpSession
2222
import org.springframework.web.util.WebUtils
23+
import javax.servlet.ServletContext
2324

2425
/**
2526
* <p>This plug-in adds methods to the Servlet API interfaces to make them more Grailsy. For example all classes that implement
@@ -54,17 +55,27 @@ class ServletsGrailsPlugin {
5455
delegate.setAttribute(name, value)
5556
}
5657
}
58+
59+
def getAttributeSubscript = { String key ->
60+
delegate.getAttribute(key)
61+
}
62+
def setAttributeSubScript = { String key, Object val ->
63+
delegate.setAttribute(key, val)
64+
}
65+
66+
// enables acces to servlet context with servletContext.foo syntax
67+
ServletContext.metaClass.getProperty = getAttributeClosure
68+
ServletContext.metaClass.setProperty = setAttributeClosure
69+
ServletContext.metaClass.getAt = getAttributeSubscript
70+
ServletContext.metaClass.putAt = setAttributeSubScript
71+
5772
// enables access to session attributes using session.foo syntax
5873
HttpSession.metaClass.getProperty = getAttributeClosure
5974
HttpSession.metaClass.setProperty = setAttributeClosure
6075
// enables access to session attributes with session["foo"] syntax
61-
HttpSession.metaClass.getAt = { String key ->
62-
delegate.getAttribute(key)
63-
}
76+
HttpSession.metaClass.getAt = getAttributeSubscript
6477
// enables setting of session attributes with session["foo"] = "bar" syntax
65-
HttpSession.metaClass.putAt = { String key, Object val ->
66-
delegate.setAttribute(key, val)
67-
}
78+
HttpSession.metaClass.putAt = setAttributeSubScript
6879

6980
// retrieve the forwardURI for the request
7081
HttpServletRequest.metaClass.getForwardURI = {->

test/groovy/org/codehaus/groovy/grails/plugins/web/ServletsGrailsPluginTests.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ class ServletsGrailsPluginTests extends AbstractGrailsPluginTests {
1616
pluginsToLoad << gcl.loadClass("org.codehaus.groovy.grails.plugins.web.ServletsGrailsPlugin")
1717
}
1818

19+
void testServletContextObject() {
20+
def context = new MockServletContext()
21+
22+
context["foo"] = "bar"
23+
assertEquals "bar", context["foo"]
24+
25+
context.foo = "fred"
26+
assertEquals "fred", context.foo
27+
28+
29+
assertEquals "fred", context.getAttribute('foo')
30+
31+
context.removeAttribute("foo")
32+
}
33+
1934
void testHttpSessionObject() {
2035
def session = new MockHttpSession()
2136

0 commit comments

Comments
 (0)