有没有办法在Play中将scala中的session对象转换为java中的session对象?
我有一个用java
编写的模型方法
,例如:
public void DoSomething(Request request, Session session)
{
String fancyValue = request.getQueryString("userInput");
session.put("Some Fancy Stuff",fancyValue);
}
和一个用 scala
编写的 Controller
方法,如下所示:
def showHomePage = Action { implicit request =>
val JRequest = play.core.j.JavaHelpers.createJavaRequest(request)
val JSession // conversion needed from request.session to play.mvc.Http.Session
new SomeModel().DoSomething(JRequest,JSession)
// would this include the updates done to the session in the java model?
Ok("Testing Stuff").withSession(session)
}
最佳答案
如果您look at the docs of the JavaHelpers
package ,你会在那里找到一个函数createJavaContext
。您可以使用它来获取 play.mvc.Http.Context
,从中您可以提取 play.mvc.Http.Request
和 play.mvc。 Http.Session
.
val java_ctx = play.core.j.JavaHelpers.createJavaContext(request)
val java_request = java_ctx.request()
val java_session = java_ctx.session()
关于java - 在 Play 框架中将 Scala session 对象转换为 Java session 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22961068/