简体   繁体   English

播放框架 - 绑定路由中的枚举

[英]play framework - bind enum in routes

I am building an app in java on play 2.2. 我正在使用java on play 2.2构建一个应用程序。

I have a java enum as a parameter in a function that I use in routes. 我有一个java枚举作为我在路由中使用的函数中的参数。

This is my enum class. 这是我的enum课程。 I searched around and found out I need to implements QueryStringBindable to use it in routes. 我四处搜索,发现我需要实现QueryStringBindable才能在路由中使用它。

public enum Something implements QueryStringBindable<Something> {
    a,
    b,
    c;

    @Override
    public F.Option<ClientStatus> bind(String key, Map<String, String[]> params) {
        String[] arr = params.get(key);
        if (arr == null || arr.length == 0) {
            return F.Option.None();
        } else {
            Something status = Something.valueOf(arr[0]);
            return F.Option.Some(status);
        }
    }

    @Override
    public String unbind(String key) {
        return null;
    }

    @Override
    public String javascriptUnbind() {
        return null;
    }
}

Yet I tried in my routes: 然而我尝试了我的路线:

GET    /someurl     controllers.Application.function(status: util.enums.Something)

But it returns bad request with error message as: 但它返回错误消息的错误请求:

For request 'GET /someurl' [util.enums.Something] 要求'GET / someurl'[util.enums.Something]

I googled and didn't find any answer working in my case. 我用谷歌搜索,并没有找到任何答案在我的情况下工作。 Did I miss something or play doesn't support binding enums? 我错过了什么或玩不支持绑定枚举?

I had the same problem and I finally found out that it is not solvable as is. 我有同样的问题,我终于发现它不能解决。

By reading the documentation for PathBindable and QueryStringBindable I found that play framework requires the Bindable to provide a No Argument public constructor. 通过阅读PathBindableQueryStringBindable的文档,我发现play框架需要 Bindable提供No Argument公共构造函数。 Which by definition is no possible with enum in Java. 根据定义,Java中的enum是不可能的。

So I had to wrap my enum to solve this. 所以我不得不用我的枚举来解决这个问题。 In your example we would have something like: 在你的例子中我们会有类似的东西:

public enum Something {
    a,
    b,
    c;

    public static class Bound implements QueryStringBindable<Bound>{
       private Something value;

        @Override
        public F.Option<ClientStatus> bind(String key, Map<String, String[]> params) {
            String[] arr = params.get(key);
            if (arr != null && arr.lenght > 0) {
                this.value = Something.valueOf(arr[0]);
                return F.Option.Some(this);
            } else {
                return F.Option.None();
            }
        }

        @Override
        public String unbind(String key) {
            return this.value.name();
        }

        @Override
       public String javascriptUnbind() {
            return this.value.name();
       }

       public Something value(){
           return this.value;
       }
    }
}

and then you have to use the type some.package.Something.Bound as a type in your routes file. 然后你必须使用some.package.Something.Bound类型作为路由文件中的类型。

EDIT: using that in a template is slightly more tricky. 编辑:在模板中使用它稍微有点棘手。 And you have to know a bit of scala. 你必须知道一点scala。 To follow @Aleksei's comment 关注@Aleksei的评论

<a href="@routes.MyController.showStuff(myEnumVar)">link</a>

should become 应该成为

<a href="@{
   routes.MyController.showStuff(new MyEnumVarWrapper(myEnumVar)).url
}">link</a>

I want to offer a small correction to the answer: (The return type isn't ClientStatus, and the unbind function should use the key parameter, it's for the url generatiion) 我想对答案提供一个小的修正:(返回类型不是ClientStatus,unbind函数应该使用key参数,它是url generatiion)

public enum Something {
a,
b,
c;

public static class Bound implements QueryStringBindable<Bound>{
   private Something value;

    @Override
    public F.Option<Bound> bind(String key, Map<String, String[]> params) {
        String[] arr = params.get(key);
        if (arr != null && arr.lenght > 0) {
            this.value = Something.valueOf(arr[0]);
            return F.Option.Some(this);
        } else {
            return F.Option.None();
        }
    }

    @Override
    public String unbind(String key) {
        return key + "=" + this.value.name();
    }

    @Override
   public String javascriptUnbind() {
        return this.value.name();
   }

   public Something value(){
       return this.value;
   }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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