Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Play 2.0 java-从http上下文获取模型列表_Java_Playframework 2.0 - Fatal编程技术网

Play 2.0 java-从http上下文获取模型列表

Play 2.0 java-从http上下文获取模型列表,java,playframework-2.0,Java,Playframework 2.0,我将模型列表存储在Http.context中,然后无法在视图中循环使用它 索引操作: @With(MembershipAction.class) public static Result index() { ... 行动组成: public class MembershipAction extends Action.Simple { @Override public Result call(Context ctx) throws Throwable { Member member =

我将模型列表存储在Http.context中,然后无法在视图中循环使用它

索引操作:

@With(MembershipAction.class)
public static Result index() {
...
行动组成:

public class MembershipAction extends Action.Simple {
@Override
public Result call(Context ctx) throws Throwable {

    Member member = Membership.getUser();
    if (member != null) {
        // MGroup.findInvolving(member)= List<play.db.ebean.Model> 
        // code : find.where().eq("members.id", member.id).findList();
        ctx.args.put("groups", MGroup.findInvolving(member));
    }
    return delegate.call(ctx);
}
}
输出: BeanList大小[6]具有更多行[false]列表[models]。MGroup@51,型号。MGroup@3d,型号。MGroup@2a,型号。MGroup@29,型号。MGroup@15,型号。MGroup@1]

模板视图#2(不工作):

@for(group
ctx().args
为您提供了一个对象的字符串映射。因此,当您从该映射中获取内容时,它的类型为
Object
。我认为您的模板编译失败,因为您试图在对象上循环

解决此问题的最快方法是在模板中执行强制转换:

@for(group <- ctx().args.get("groups").asInstanceOf[List[models.MGroup]]) {
  @group.name
}

@for(组)感谢它的工作。我知道以前“@Menus”解决方案不起作用的原因。我需要在视图中导入Action类并结合一个静态方法来返回列表:
public static list groups(){return(list)Http.Context.current().args.get(“groups”);}
view snippet(组好,我知道你在做什么。你在
MembershipAction
上添加了一个方便的方法,用于检索添加到上下文映射中的模型对象。这也是一个很好的方法。
@for(group <- ctx().args.get("groups") ){
    @group.name
}
@for(group <- ctx().args.get("groups").asInstanceOf[List[models.MGroup]]) {
  @group.name
}