org.scalatra.UrlSupport.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalatra_2.8.2.RC1 Show documentation
Show all versions of scalatra_2.8.2.RC1 Show documentation
The core Scalatra framework
The newest version!
package org.scalatra
import java.net.URLEncoder.encode
import javax.servlet.http.HttpServletResponse
/**
* Provides utility methods for the creation of URL strings.
* Supports context-relative and session-aware URLs. Should behave similarly to JSTL's tag.
*/
trait UrlSupport {
protected def contextPath: String
protected def response: HttpServletResponse
def url(path: String): String = url(path, Seq.empty)
def url(path: String, params: Iterable[(String, Any)]): String = {
val newPath = path match {
case x if x.startsWith("/") => contextPath + path
case _ => path
}
val pairs = params map { case(key, value) => encode(key, "utf-8") + "=" +encode(value.toString, "utf-8") }
val queryString = if (pairs.isEmpty) "" else pairs.mkString("?", "&", "")
response.encodeURL(newPath+queryString)
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy