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

org.beangle.webmvc.api.context.ActionContext.scala Maven / Gradle / Ivy

The newest version!
package org.beangle.webmvc.api.context

import java.{ util => ju }
import org.beangle.commons.collection.Collections
import org.beangle.commons.text.i18n.TextResource
import javax.servlet.http.{ HttpServletRequest, HttpServletResponse }

object ContextHolder {
  val contexts = new ThreadLocal[ActionContext]
  def context: ActionContext = contexts.get()
}

final class ActionContext(val request: HttpServletRequest, var response: HttpServletResponse, val locale: ju.Locale, val params: Map[String, Any]) {

  var textResource: TextResource = _

  private var flashMap: Flash = _

  private val temp = new collection.mutable.HashMap[String, Any]

  def attribute(name: String, value: Any): Unit = {
    request.setAttribute(name, value)
  }

  def removeAttribute(names: String*) {
    names foreach { name =>
      request.removeAttribute(name)
    }
  }

  def attribute[T](name: String): T = {
    request.getAttribute(name).asInstanceOf[T]
  }

  def temp(name: String, value: Any): Unit = {
    temp.put(name, value)
  }

  def temp[T](name: String): T = {
    temp.get(name).orNull.asInstanceOf[T]
  }

  def flash: Flash = {
    if (null == flashMap) {
      val session = request.getSession()
      val flashObj = session.getAttribute("flash")
      if (null != flashObj) flashMap = flashObj.asInstanceOf[Flash]
      else {
        flashMap = new Flash
        session.setAttribute("flash", flashMap)
      }
    }
    flashMap
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy