리프트 스칼라에 세션 변수를 저장합니다
문제
세션 변수를 저장 한 다음 사용하여 Boot.scala에서 메뉴를 수정합니다. 스 니펫에 변수를 저장하는 방법은 다음과 같습니다.
object sessionUserType extends SessionVar[String](null)
def list (xhtml : NodeSeq) : NodeSeq = {
Helpers.bind("sendTo", xhtml,
"provider" -> SHtml.link("/providerlogin",() => sessionUserType("provider"), Text("Provider")),
"student" -> SHtml.link("/studentlogin",() => sessionUserType("student"), Text("Student")))
}
그런 다음 boot.scala에서 나는 이것을한다 :
val studentSessionType = If(() => S.getSessionAttribute("sessionUserType").open_!.equals("student"),
"not a student session")
나는 또한 이름으로 객체를 호출하려고 시도했지만 (sessionusertype), 그것을 찾을 수 없으므로 이것이 효과가 있다고 생각했지만 실제 바인딩과 기능이 메뉴 렌더링.
어떤 도움이든 큰 감사를드립니다.
감사
해결책
값을 얻기 위해 SessionVar
또는 RequestVar
, 전화 is
그것에 대한 방법, 즉 sessionUserType.is
그건 그렇고, 당신은 읽었습니까?상태 관리"?
참고 사항
나는 믿는다 RequestVar
귀하의 경우에 더 잘 맞습니다. 컨텍스트없이 귀하의 코드를 올바르게 잡을 수 있는지 확실하지 않지만 적어도 다음과 같이 다시 작성할 수 있습니다.
case class LoginType
case object StudentLogin extends LoginType
case object ProviderLogin extends LoginType
object loginType extends RequestVar[Box[LoginType]](Empty)
// RequestVar is a storage with request-level scope
...
"provider" -> SHtml.link("/providerlogin",() => loginType(Full(ProviderLogin)), Text("Provider")),
// `SHtml.link` works in this way. Your closure will be called after a user
// transition, that is when /providerlogin is loading.
...
val studentSessionType = If(() => { loginType.is map {_ == StudentLogin} openOr false },
"not a student session")
// As a result, this test will be true if our RequestVar holds StudentLogin,
// but it will be so for one page only (/studentlogin I guess). If you want
// scope to be session-wide, use SessionVar
다른 팁
나는 단절이 당신이 세션 속성 당신과 일치 할 것입니다 세션 바, 그리고 그것은 사실이 아닙니다. 리프트는 매우입니다 안전한 프레임 워크, 그리고 그 기능은 리프트가 서버 측의 구성 요소를 참조하기 위해 불투명 한 안내서를 생성한다는 것입니다.
네가 원한다면 getSessionAttribute
무언가를 돌려 보려면 어떻게 전화 할 수 있는지 생각해보십시오. setSessionAttribute
.
제휴하지 않습니다 StackOverflow