質問

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>
  }
}
役に立ちましたか?

解決

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top