简体   繁体   中英

Play2 Framework - Secure Social Session Value

at my current Project I'm using the secure social plugin for Play 2 Framework. I wrote a function to count my Products which are stored in the Session.

    public static Integer cntOrderedProductsFromSession(){

    Integer cnt = 0;
    try {
        List<String> KeyList = new ArrayList<String>(session().keySet());
    for (Iterator<String> iterator = KeyList.iterator(); iterator.hasNext();){
        String name = iterator.next();
        if(name.matches("product.[0-9]*")){
            cnt += Integer.parseInt(session(name));
        }
    }
    return cnt;
    } catch (RuntimeException e){
        return 0;
    }
}

I wrapped my secure social login Page in a main.hmtl.scala, here I call my function

<p>@Shop.cntOrderedProductsFromSession()</p>

The problem is, that I get an error, when I try use the cntOrderedProductsFromSession() function. (But only with the secure social login Page)

I get this error

 java.lang.RuntimeException: There is no HTTP Context available from here.
at play.mvc.Http$Context.current(Http.java:30) ~[play_2.10.jar:2.2.3]
at play.mvc.Controller.session(Controller.java:60) ~[play_2.10.jar:2.2.3]
at controllers.Shop.cntOrderedProductsFromSession(Shop.java:236) ~[classes/:na]
at views.html.main$.apply(main.template.scala:173) ~[classes/:na]
at views.html.custom.login$.apply(login.template.scala:50) ~[classes/:na]
at plugins.NekViews.getLoginPage(NekViews.scala:32) ~[classes/:na]
at securesocial.controllers.LoginPage$$anonfun$login$1.apply(LoginPage.scala:56) ~[securesocial_2.10-2.1.3.jar:2.1.3]
at securesocial.controllers.LoginPage$$anonfun$login$1.apply(LoginPage.scala:42) ~[securesocial_2.10-2.1.3.jar:2.1.3]
at play.api.mvc.ActionBuilder$$anonfun$apply$10.apply(Action.scala:221) ~[play_2.10.jar:2.2.3]
at play.api.mvc.ActionBuilder$$anonfun$apply$10.apply(Action.scala:220) ~[play_2.10.jar:2.2.3]
at play.api.mvc.Action$.invokeBlock(Action.scala:357) ~[play_2.10.jar:2.2.3]
at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:309) ~[play_2.10.jar:2.2.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:109) ~[play_2.10.jar:2.2.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:109) ~[play_2.10.jar:2.2.3]
at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.2.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:108) ~[play_2.10.jar:2.2.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:107) ~[play_2.10.jar:2.2.3]
at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:107) ~[play_2.10.jar:2.2.3]
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:100) ~[play_2.10.jar:2.2.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:481) ~[play-iteratees_2.10.jar:2.2.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:481) ~[play-iteratees_2.10.jar:2.2.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:517) ~[play-iteratees_2.10.jar:2.2.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:517) ~[play-iteratees_2.10.jar:2.2.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:493) ~[play-iteratees_2.10.jar:2.2.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:493) ~[play-iteratees_2.10.jar:2.2.3]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library.jar:na]

Thaks 4 help

The error is quite explicit. There is no HTTP Context available from here. You will have to pass the context to all your views/functions using the context.

Within a template, that can be done as follows :

@(param1:Param1Type)(implicit context:Context) 

@main("Title") {
}

Where in the main.scala.html template you will have the following :

@main(title:String)(implicit context:Context)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM