2

I'm asking this as a relatively new person to Scala. I've seen examples of how to create 'control constructs' in Scala, but I don't know it well enough to follow the code of Scalatra itself.

Can someone please explain how 'params' is passed to the defined route handlers? I feel like that's a useful technique and I'd like to know how it's done.

Thank you!

Edit: Adding in the sample code from the Scalatra website to illustrate what I'm talking about:

class HelloWorldApp extends ScalatraFilter {
  get("/") {
    <h1>Hello, {params("name")}</h1>
  }
}
Dave Kapp
  • 301
  • 1
  • 7

2 Answers2

3

These are the traits involved:

trait ScalatraFilter extends Filter with ServletBase
trait ServletBase extends ScalatraBase with SessionSupport with Initializable
trait ScalatraBase extends ScalatraContext with CoreDsl with DynamicScope.....

when using params you are using one of the few overloaded methods defined in ScalatraBase

def params(key: String)(implicit request: HttpServletRequest): String = params(request)(key)
def params(key: Symbol)(implicit request: HttpServletRequest): String = params(request)(key)
def params(implicit request: HttpServletRequest): Params = new ScalatraParams(multiParams)

check the code

https://github.com/scalatra/scalatra/blob/develop/core/src/main/scala/org/scalatra/ScalatraBase.scala https://github.com/scalatra/scalatra/blob/develop/core/src/main/scala/org/scalatra/ScalatraFilter.scala https://github.com/scalatra/scalatra/blob/develop/core/src/main/scala/org/scalatra/servlet/ServletBase.scala

mericano1
  • 2,914
  • 1
  • 18
  • 25
  • 1
    Aha, I thought it was a variable being passed in. Making it a method makes sense. Thanks for the information and the links! – Dave Kapp Feb 27 '13 at 22:55
0

It's done by using Scala's DynamicVariable class. Here's a short blog post that explains it really well.

http://www.riffraff.info/2009/4/11/step-a-scala-web-picoframework

user43624
  • 188
  • 1
  • 10