All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalatra.UrlSupport.scala Maven / Gradle / Ivy

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